From: php at richardneill dot org Operating system: all PHP version: 5.4.13 Package: PCRE related Bug Type: Feature/Change Request Bug description:wish: add flag to preg_quote to escape the replacement
Description: ------------ preg_quote() is great for escaping a user-supplied search string. There is no matching function to escape a user-supplied replacement string. My wish is for preg_quote() to have an extra flag "IS_REPLACEMENT" or to have a function preg_quote_replacement(), which would escape only backslash and dollar signs. Example below. Might I also suggest this is a documentation bug, in that there is no explanation of the correct way to work around this? Test script: --------------- $text = 'Invoice: You owe me *#5* !'; $user_search = "*#5*"; $user_replace = "*$5*"; $search = preg_quote($user_search, "/"); $replace_bad1 = $user_replace; $replace_bad2 = preg_quote($user_replace); $new_text1 = preg_replace("/$search/", "$replace_bad1", $text); $new_text2 = preg_replace("/$search/", "$replace_bad2", $text); echo "Input: $text\nNew1: $new_text1\nNew2: $new_text2\n"; Expected result: ---------------- I want to return the string: Invoice: You owe me *$5* ! Actual result: -------------- Input: Invoice: You owe me *#5* ! New1: Invoice: You owe me ** ! New2: Invoice: You owe me \*$5\* ! 1. the '*' in the search string is not magic, thanks to the normal use of preg_quote(). This is what I expect. 2. If I replace with a literal, then '$5' becomes the 5th backreference, which is empty. 3. If I preg_quote the replacement, then we get spurious backslashes before the '*'s. -- Edit bug report at https://bugs.php.net/bug.php?id=64516&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=64516&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=64516&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=64516&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=64516&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=64516&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=64516&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=64516&r=needscript Try newer version: https://bugs.php.net/fix.php?id=64516&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=64516&r=support Expected behavior: https://bugs.php.net/fix.php?id=64516&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=64516&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=64516&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=64516&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64516&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=64516&r=dst IIS Stability: https://bugs.php.net/fix.php?id=64516&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=64516&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=64516&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=64516&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=64516&r=mysqlcfg