Brian W Posted December 30, 2008 Share Posted December 30, 2008 Regular expressions irritate the living shit out of me. Though what I have doesn't give me some error, it just doesn't do anything, so I'm lost. What I want it to do is replace " "(white space) with "_" for any text within quotes. $Cleaned = preg_replace("/(\"[a-zA-Z0-9]\")/", str_replace(" ", "_", "$1"), $string); but like I said, nothing happens to the string. I've been testing with $string = "\"Word Word\"" any help is appreciated, thanks Quote Link to comment Share on other sites More sharing options...
effigy Posted December 30, 2008 Share Posted December 30, 2008 Character classes only match one character. You must add a quantifier to match more. The replacement needs to be evaluated in order to act on the matched data. preg_replace('/("[^"]+")/e', 'str_replace(" ", "_", "$1")', $string); Quote Link to comment Share on other sites More sharing options...
Brian W Posted December 30, 2008 Author Share Posted December 30, 2008 Thanks effigy. I don't quite understand how that worked, but it does. I've been using http://www.webcheatsheet.com/php/regular_expressions.php to learn about regular expressions but still not getting it. Quote Link to comment Share on other sites More sharing options...
effigy Posted December 30, 2008 Share Posted December 30, 2008 You may want to try some other tutorials via our resources. These two paragraphs from your link cover character classes and quantifiers: To specify a set of acceptable characters in your pattern, you can either build a character class yourself or use a predefined one. A character class lets you represent a bunch of characters as a single item in a regular expression. You can build your own character class by enclosing the acceptable characters in square brackets. A character class matches any one of the characters in the class. For example a character class [abc] matches a, b or c. To define a range of characters, just put the first and last characters in, separated by hyphen. For example, to match all alphanumeric characters: [a-zA-Z0-9]. You can also create a negated character class, which matches any character that is not in the class. To create a negated character class, begin the character class with ^: [^0-9]. The metacharacters +, *, ?, and {} affect the number of times a pattern should be matched. + means "Match one or more of the preceding expression", * means "Match zero or more of the preceding expression", and ? means "Match zero or one of the preceding expression". Curly braces {} can be used differently. With a single integer, {n} means "match exactly n occurrences of the preceding expression", with one integer and a comma, {n,} means "match n or more occurrences of the preceding expression", and with two comma-separated integers {n,m} means "match the previous character if it occurs at least n times, but no more than m times". And here's a breakdown of the pattern: NODE EXPLANATION ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- [^"]+ any character except: '"' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.