I've been trying to get to grips with escaping backlashes so I wrote the following test script and used php cli to see where PCRE complained about an incorrect number of backslashes. In my php.ini file, I used
error_reporting = E_ALL | E_STRICT
I used '#' has the delimiter for the regexp.
<?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 " . $php_errormsg . "\n";
}
/*
echo preg_match('#\\\#', "any"); ==> no errors
unlike for $i==3 in the loop above
*/
As the comment just above says, I get no error if I use 3 slashes in a script but I do when I build the pattern up and test it in the loop of the script.
As you can see in the script, if there is no error, I don't output a message. Here is the output:
[CODE]
Number of '\' is 1
Pattern is: ##
ERROR preg_match(): No ending delimiter '#' found
Number of '\' is 2
Pattern is: #\#
Number of '\' is 3
Pattern is: #\#
ERROR preg_match(): No ending delimiter '#' found
Number of '\' is 4
Pattern is: #\\#
Number of '\' is 5
Pattern is: #\\#
ERROR preg_match(): No ending delimiter '#' found
Number of '\' is 6
Pattern is: #\\\#
Number of '\' is 7
Pattern is: #\\\#
ERROR preg_match(): No ending delimiter '#' found
Number of '\' is 8
Pattern is: #\\\\#
Number of '\' is 9
Pattern is: #\\\\#
ERROR preg_match(): No ending delimiter '#' found
Number of '\' is 10
Pattern is: #\\\\\#
[/CODE]
$i==3 is not the only difference I found.
For e.g. the following in a script does not throw an error:
<?php
echo preg_match('#\\\\\\\#', "any");
This has seven backslashes which should show an error according to the code that runs in a loop in my test script.
Any ideas about what I'm doing wrong? Thanks.
Lucas