Doc #61656 [NEW]: Missing hint about backslash modifier/escaping

From: Date: Sat, 07 Apr 2012 02:23:32 +0000
Subject: Doc #61656 [NEW]: Missing hint about backslash modifier/escaping
Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-8211@lists.php.net to get a copy of this message
From: Operating system: PHP version: Irrelevant Package: Documentation problem Bug Type: Documentation Problem Bug description:Missing hint about backslash modifier/escaping 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 bug report at https://bugs.php.net/bug.php?id=61656&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=61656&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=61656&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=61656&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=61656&r=fixed Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=61656&r=needdocs Fixed in release: https://bugs.php.net/fix.php?id=61656&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=61656&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=61656&r=needscript Try newer version: https://bugs.php.net/fix.php?id=61656&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=61656&r=support Expected behavior: https://bugs.php.net/fix.php?id=61656&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=61656&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=61656&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=61656&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=61656&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=61656&r=dst IIS Stability: https://bugs.php.net/fix.php?id=61656&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=61656&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=61656&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=61656&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=61656&r=mysqlcfg

« previous php.doc.bugs (#8211) next »