|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #61656 Missing hint about backslash modifier/escaping
Submitted: 2012-04-07 02:23 UTC Modified: 2012-04-07 19:23 UTC
From: hardcorevenom at gmx dot de Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: hardcorevenom at gmx dot de
New email:
PHP Version: OS:


 [2012-04-07 02:23 UTC] hardcorevenom at gmx dot de
I had some difficulties with preg_match() trying to match a backslash.
It took me some time to find out it is '\\\\' in php code.
I'm missing a note or example about double escaping in the documentation.

Something like [see test script] would have made it clear for me in the first place and probably will help others.

Odd issue:

//Here the ']' is escaped, but it doesn't return the error: "missing terminating ]", and accepts without complaining about the syntax error:
preg_match('/^[a-z0-9_.\/\\\]*$/i', $file_string);
//while here we get an error report:
preg_match('/^[a-z0-9_.\/\]*$/i', $file_string);

Test script:
//match newline control character:
preg_match('/\n/','\n');   //pattern matches and is stored as control character 0x0A in the pattern string
preg_match('/\\\n/','\n'); //same match, but is stored escaped as 0x5C,0x6E in the pattern string
//people mostly use the upper variant

//trying to match "\'" (2 characters) in a text file, '\\\'' as php string:
$subject = file_get_contents('myfile.txt');
preg_match('/\\\'/',$subject);   //DOESN'T MATCH! stored as 0x5C,0x27 (escaped apostrophe) this only matches apostrophe
preg_match('/\\\\\'/',$subject); //matches, stored as 0x5C,0x5C,0x27 (escaped backslash and unescaped apostrophe)
preg_match('/\\\\\\\/',$subject); //also matches, stored as 0x5C,0x5C,0x5C,0x27 (escaped backslash and escaped apostrophe)

//matching "\n" (2 characters):
preg_match('/\\\\n'/','\\n');  //match

Expected result:
Example or note in the documentation

Actual result:
A confused user, not realizing how to escape correctly for preg_match()


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-07 12:52 UTC] phpmpan at mpan dot pl
String literals, that are later passed to `preg_match`, follow exactly the same rules, as all other string literals in PHP. There is nothing special about them. Strings syntax is described in the manual [1].

 [2012-04-07 19:23 UTC]
As mentioned, this is a basic matter of string handling in PHP, and is unrelated 
to regex. As such, it's "not a bug".
 [2012-04-07 19:23 UTC]
-Status: Open +Status: Not a bug
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Apr 20 08:20:13 2021 UTC