note 49362 added to function.preg-replace
| From: | jw-php at valleyfree dot com | Date: | Tue, 25 Jan 2005 16:28:48 +0000 |
| Subject: | note 49362 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-83931@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49362
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49362&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49362&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49362&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49362&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49362&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49362&report=yes
Search -- http://master.php.net/manage/user-notes.php