php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11540 Backslash and regular expressions
Submitted: 2001-06-18 11:53 UTC Modified: 2001-06-20 06:39 UTC
From: oroos at fmlogistic dot pl Assigned:
Status: Not a bug Package: *Regular Expressions
PHP Version: 4.0.5 OS: Windows
Private report: No CVE-ID: None
 [2001-06-18 11:53 UTC] oroos at fmlogistic dot pl
Hello,

I have to write a program which replaces backslashes by slashes into HTML code. I would like to do this using regular expressions.

Unfortunately, there may be a problem in pattern matching as PHP does not seem to work properly as soon as I add some backslashes in my patterns...

Here is a small example which illustrates the problem:
<?
$test = "X\\X"; // $test is a string containing a backslash
echo $test."\n";
echo preg_quote($test)."\n";
echo preg_match("/X\\X/", $test)."\n";
?>

The output of this program is the following:
X\X
X\\X
0

The '0' means that the preg_match failed... But the pattern of this preg_match was the string $test quoted by preg_quote to become a valid pattern... (as you can see on second line of the output).
So this preg_match should output '1', shouldn't it ??

Thanks in advance for any help or advice,
Olivier Roos

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-06-20 03:02 UTC] oroos at fmlogistic dot pl
I am told that I should put four backslahes in my pattern to make it match a single backslash.

This is good in static patterns. But my pattern is a variable string which I want to put in good pattern format using the fonction preg_quote.

But this function transforms one backslash in only two backshlashes (and not 4 !)... Thus, the only way to make my program work is to apply two times this function preg_quote.  This works, but this is not very nice, is it ?

So, isn't there a bug in function preg_quote ?

Thanks in advance,
Olivier Roos

 [2001-06-20 05:55 UTC] oroos at fmlogistic dot pl
Sorry my added comment is silly... I just understood why I have to put "\\\\" in my source code and why it is enough that preg_quote generates two backslashes...

So there is no bug at all.

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Jun 16 17:01:30 2019 UTC