Doc #61656 [Com]: Missing hint about backslash modifier/escaping
| From: | phpmpan at mpan dot pl | Date: | Sat, 07 Apr 2012 12:52:56 +0000 |
| Subject: | Doc #61656 [Com]: Missing hint about backslash modifier/escaping | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-8213@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=61656&edit=1
ID: 61656
Comment by: phpmpan at mpan dot pl
Reported by: hardcorevenom at gmx dot de
Summary: Missing hint about backslash modifier/escaping
Status: Open
Type: Documentation Problem
Package: Documentation problem
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2012-04-07 02:23:32] 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()
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61656&edit=1