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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
31 - 26 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 01:01:28 2024 UTC