Re: php seems to be inconsistent in its handling of backslashes ... maybe?
| From: | Al | Date: | Sun, 22 Apr 2007 16:19:29 +0000 |
| Subject: | Re: php seems to be inconsistent in its handling of backslashes ... maybe? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-253491@lists.php.net to get a copy of this message | ||
The proper way to handle special control PCRE characters like "\" is to use the hex [e.g., \x5C] value. Then you won't have a problem. The engine knows you want the the object treated as a character and an a control.
ufan100@gmail.com wrote:
-- 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.