On Fri, 9 Jan 2004, craig wrote:
> (4536,'golf tournament management',430,0,0),
> (1434,'Premium golf balls',,,0),
> 
> I have to replace the blank entries (,,) with NULLs, using this regex:
> $query = preg_replace('/,\s*,/',',NULL,', $query, -1);
> after this line, only ONE of the ,, sets is replaced by ,NULL, like:
> (1434,'Premium golf balls',NULL,,0)

The regex does continue trying to make matches, but the point at which it
continues is just past your replacement. In other words, the trailing
comma in ,NULL, is not considered part of the string to match/replace.

This should do the trick:
preg_replace('/,\s*(?=[,\)])/', ',NULL', $input);

(?=pattern) is a positive lookahead. It evaluates true if the next 
characters match the pattern, but those characters are not consumed.

So that regex is equivalent to "match a pattern starting with a comma 
followed by any existing spaces, ONLY IF the next character is , or )"

The most robust way you could write this regex is:
preg_replace('/([,\(])\s*(?=[,\)])/', '\1NULL', $input);

I know you'll probably never have input like (,,,,),
but it would work as expected.

Many tricky regex problems can be solved by lookaheads. There is also a
negative lookahead (?!pattern) ... also note that this is an advanced
regex feature and won't it work on many regex engines not based on PCRE.

-- 
Kelly Hallman
// Ultrafancy

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to