note 49362 deleted from function.preg-replace by colder
| From: | colder@php.net | Date: | Mon, 18 Jun 2007 19:52:09 +0000 |
| Subject: | note 49362 deleted from function.preg-replace by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127671@lists.php.net to get a copy of this message | ||
Note Submitter: jw-php at valleyfree dot com
----
note the that if you want to replace all backslashes in a string with double backslashes (like
addslashes() does but just for backslashes and not quotes, etc), you'll need the following:
$new = preg_replace('/\\\\/','\\\\\\\\',$old);
note the pattern uses 4 backslashes and the replacement uses 8! the reason for 4 slashses in the
pattern part has already been explained on this page, but nobody has yet mentioned the need for the
same logic in the replacement part in which backslashes are also doubly parsed, once by PHP and once
by the PCRE extension. so the eight slashes break down to four slashes sent to PCRE, then two
slashes put in the final output.