php.net |  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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hardcorevenom at gmx dot de
New email:
PHP Version: OS:

 

 [2012-04-07 02:23 UTC] hardcorevenom at gmx dot de
Description:
------------
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:
http://www.php.net/manual/en/function.preg-match.php#101575

//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()

Patches

Pull Requests

History

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].

[1] http://www.php.net/manual/en/language.types.string.php
 [2012-04-07 19:23 UTC] frozenfire@php.net
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] frozenfire@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Sep 17 03:00:01 2025 UTC