Page 1 of 1
Regular Expression Woes (bug in PHP ?)
Posted: Wed Nov 25, 2009 2:30 pm
by JasonHandle
I'm having a bit of an issue with a rather simple regular expression - perhaps someone can help point me in the right direction?
I have a test string: "here is a simple test". When I pass this string into my regexp a blank string is returned (I'm guessing it's actually returning NULL). Running PHP 5.2.5
And here is the simple regexp:
$tempString = preg_replace("/[\\]{3,}/", "\\", $tempString);
Does anyone know what is going on?
Re: Regular Expression Woes
Posted: Wed Nov 25, 2009 2:45 pm
by superdezign
You shouldn't need character brackets. And are you sure that the input is not blank as well? Check it.
Also, try single quotes to see if it makes a difference. It shouldn't, but PHP treats curly braces as a special character (contextually) sometimes, so it might.
Re: Regular Expression Woes
Posted: Wed Nov 25, 2009 3:14 pm
by JasonHandle
superdezign wrote:You shouldn't need character brackets. And are you sure that the input is not blank as well? Check it.
Also, try single quotes to see if it makes a difference. It shouldn't, but PHP treats curly braces as a special character (contextually) sometimes, so it might.
ok I just tried:
Code: Select all
$testText = "this is sample test text";
echo "$testText | ";
$testText = preg_replace("/\\/", "LA LA LA", $testText);
echo "$testText";
and the output was exactly "this is sample test text | "
*edit*
so I changed my code around to print out the last preg error if the return value is NULL
Code: Select all
$testText = "this is sample test text";
echo "$testText | ";
$testText2 = preg_replace("/\\/", "LA LA LA", $testText);
if ($testText2 === NULL) {
switch (preg_last_error()) {
case PREG_NO_ERROR:
echo "pcre_error: PREG_NO_ERROR!\n";
break;
case PREG_INTERNAL_ERROR:
echo "pcre_error: PREG_INTERNAL_ERROR!\n";
break;
case PREG_BACKTRACK_LIMIT_ERROR:
echo "pcre_error: PREG_BACKTRACK_LIMIT_ERROR!\n";
break;
case PREG_RECURSION_LIMIT_ERROR:
echo "pcre_error: PREG_RECURSION_LIMIT_ERROR!\n";
break;
case PREG_BAD_UTF8_ERROR:
echo "pcre_error: PREG_BAD_UTF8_ERROR!\n";
break;
case PREG_BAD_UTF8_OFFSET_ERROR:
echo "pcre_error: PREG_BAD_UTF8_OFFSET_ERROR!\n";
break;
}
} else
echo "$testText2";
It prints out: "this is sample test text | pcre_error: PREG_NO_ERROR!"
I don't understand what is going on.
Re: Regular Expression Woes (bug in PHP ?)
Posted: Wed Nov 25, 2009 7:29 pm
by JasonHandle
No one else has any ideas?
Re: Regular Expression Woes (bug in PHP ?)
Posted: Wed Nov 25, 2009 9:04 pm
by John Cartwright
Turn on error reporting, i.e.,
You should have seen an error along the lines of No ending delimter / found. This is because your escaping the delimeter. Your pattern would become:
Code: Select all
preg_replace("/\\\/", "LA LA LA", $testText);
You might want consider a less common delimeter, such as ~ or #
Re: Regular Expression Woes (bug in PHP ?)
Posted: Thu Nov 26, 2009 3:09 pm
by superdezign
Try using four slashes instead of two. Regex tends to want additional escaping of characters.
Like, it's its own language in the language. Which it very well might be. I wouldn't know.
