Regular Expression Woes (bug in PHP ?)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JasonHandle
Forum Newbie
Posts: 3
Joined: Wed Nov 25, 2009 2:26 pm

Regular Expression Woes (bug in PHP ?)

Post 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?
Last edited by JasonHandle on Wed Nov 25, 2009 3:52 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Regular Expression Woes

Post 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.
JasonHandle
Forum Newbie
Posts: 3
Joined: Wed Nov 25, 2009 2:26 pm

Re: Regular Expression Woes

Post 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.
JasonHandle
Forum Newbie
Posts: 3
Joined: Wed Nov 25, 2009 2:26 pm

Re: Regular Expression Woes (bug in PHP ?)

Post by JasonHandle »

No one else has any ideas?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Regular Expression Woes (bug in PHP ?)

Post by John Cartwright »

Turn on error reporting, i.e.,

Code: Select all

error_reporting(E_ALL);
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 #
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Regular Expression Woes (bug in PHP ?)

Post 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. :P
Post Reply