php seems to be inconsistent in its handling of backslashes ... maybe?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



-- or maybe it's just the PCRE extension
-- or quite likely I have got something wrong

Hello members,
  I'm hoping you could enlighten me.

Using error_reporting = E_ALL | E_STRICT, I tested the following statements:

<?php
preg_match('#\\#','any-string'); => warning
preg_match('#\\\#','any-string');
preg_match('#\\\\#','any-string');
preg_match('#\\\\\#','any-string'); => warning
preg_match('#\\\\\\#','any-string'); => warning
preg_match('#\\\\\\\#','any-string');
?>

This seemed strange:
  warnings with 2 and 6 backlashes
  no warnings with 3, 7
  warning with 5 but not with 3 and 7.
The warning related of course to no matching delimeter '#' being found.

So I wrote a little test script (preg.php) to test up to 10 backslashes:

<?php
for($i=1; $i<=10; ++$i) {
    echo "\n---------------------------------\n";
    echo "Number of '\\' is $i \n";
    $bs = '#';
    $bs .=  str_repeat('\\',$i);
    $bs .= '#';
    echo 'Pattern is: ' . $bs . "\n";
    $php_errormsg = "";
    @preg_match($bs, "anystring") . "\n";
    if($php_errormsg != '')
        echo "error\n";
    else echo "ok\n";
}
?>

Here is the output:

$ php preg.php

---------------------------------
Number of '\' is 1
Pattern is: #\#
error

---------------------------------
Number of '\' is 2
Pattern is: #\\#
ok

---------------------------------
Number of '\' is 3
Pattern is: #\\\#
error

---------------------------------
Number of '\' is 4
Pattern is: #\\\\#
ok

---------------------------------
Number of '\' is 5
Pattern is: #\\\\\#
error

---------------------------------
Number of '\' is 6
Pattern is: #\\\\\\#
ok

---------------------------------
Number of '\' is 7
Pattern is: #\\\\\\\#
error

---------------------------------
Number of '\' is 8
Pattern is: #\\\\\\\\#
ok

---------------------------------
Number of '\' is 9
Pattern is: #\\\\\\\\\#
error

---------------------------------
Number of '\' is 10
Pattern is: #\\\\\\\\\\#
ok

End of output.

This agrees with my understanding of backslash escaping (I hope that's right) but now I can't understand why I got the results earlier (shown in my first script).

Many thanks.

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux