tags:

views:

59

answers:

3

hey can i clean up a preg_match in php from this:

preg_match_all("/(".$this->reg['wat'].")?(".$this->reg['wat'].")?(".$this->reg['wat'].")?(".$this->reg['wat'].")?(".$this->reg['wat'].")?(".$this->reg['wat'].")?(".$this->reg['wat'].")?/",$value,$match);

to look like this:

preg_match_all("/
   (".$this->reg['wat'].")?
   (".$this->reg['wat'].")?
   (".$this->reg['wat'].")?
   (".$this->reg['wat'].")?
   (".$this->reg['wat'].")?
   (".$this->reg['wat'].")?
   (".$this->reg['wat'].")?
/",$value,$match);

right now each space, it counts as a ling break so it wont return any finds when searching. but it just looks cleaner and easier to read is why i ask you know. i was looking for one of those letters to add after the closing "/" in the regex. thanks

+3  A: 

Yes, take a look at the PCRE_EXTENDED option:

x (PCRE_EXTENDED)
If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.
VolkerK
well this is inside a class so is that why its not working? i added it in and it doesn't find any results.
David
classes have nothing to do with how PCRE_EXTENDED works.
VolkerK
+2  A: 
preg_match_all('/('.$this->reg['wat'].'){0,7}/', $value, $match);

That should be fairly clean.

erisco
i just typed this on the fly, not that simple
David
Then you need to correctly define how simple it is, as I will not give you an answer that is unnecessarily convoluted.
erisco
A: 

In case it wasn't obvious: (and as an extension/simplified version of VolkerK's answer) add the x flag to the preg_match call:

preg_match_all(".../x",$value,$match);

This will allow you to use newlines without regex actually matching them.

hlissner