Re: preg_replace_callback

From: Date: Fri, 05 May 2006 00:47:22 +0000
Subject: Re: preg_replace_callback
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-235588@lists.php.net to get a copy of this message
On Thu, May 4, 2006 8:03 am, tJey wrote: > Hi. I have problem with preg_replace_callback. It seems that my > pattern > is bad, but I can't find any error. > > Pattern : \[\s*((\d|\w|_)+)\s*\] > this pattern is intended to find strings like "[field]", > "[ fi12_eld]"... > > but every time I get warning message > "Warning: preg_replace_callback(): Delimiter must not be alphanumeric > or > backslash in test.php on line 100" The first and last character in preg_*('...') have to be delimiters for the pattern, to distinguish it from flags at the end. EG: $pattern = '/\[\s*((\d|\w|_)+)\s*\]/'; You might, for example, want to tack on mUs or even i (though that would be silly here for something like: $pattern = '/\[\s*((\d|\w|_)+)\s*\]/sU'; Also note that \ is a 'special' character inside of ' for PHP, so SOME PHP developers thinks this is "better style": $pattern = '/\\[\\s*((\\d|\\w|_)+\\s*\\]/'; PHP "eats" the \\ to feed \ to PCRE which "eats" those up to get the special characters it wants. Also, I *think* maybe the \ in front of ] is not needed, as there is no opening [ to "confuse" PCRE in the first place, since you escaped that. -- Like Music? http://l-i-e.com/artists.htm

« previous php.general (#235588) next »