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?
Regular Expression Woes (bug in PHP ?)
Moderator: General Moderators
-
JasonHandle
- Forum Newbie
- Posts: 3
- Joined: Wed Nov 25, 2009 2:26 pm
Regular Expression Woes (bug in PHP ?)
Last edited by JasonHandle on Wed Nov 25, 2009 3:52 pm, edited 1 time in total.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Regular Expression Woes
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.
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.
-
JasonHandle
- Forum Newbie
- Posts: 3
- Joined: Wed Nov 25, 2009 2:26 pm
Re: Regular Expression Woes
ok I just tried: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.
Code: Select all
$testText = "this is sample test text";
echo "$testText | ";
$testText = preg_replace("/\\/", "LA LA LA", $testText);
echo "$testText";
*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";
I don't understand what is going on.
-
JasonHandle
- Forum Newbie
- Posts: 3
- Joined: Wed Nov 25, 2009 2:26 pm
Re: Regular Expression Woes (bug in PHP ?)
No one else has any ideas?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Regular Expression Woes (bug in PHP ?)
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:
You might want consider a less common delimeter, such as ~ or #
Code: Select all
error_reporting(E_ALL);Code: Select all
preg_replace("/\\\/", "LA LA LA", $testText);- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Regular Expression Woes (bug in PHP ?)
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.
Like, it's its own language in the language. Which it very well might be. I wouldn't know.