note 52135 added to function.preg-replace
| From: | bens at effortlessis dot com | Date: | Thu, 21 Apr 2005 23:59:21 +0000 |
| Subject: | note 52135 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88375@lists.php.net to get a copy of this message | ||
It's important to note how preg_match actually works when processing arrays - it's the
same as running preg_match repeatedly in serial.
Here's a demonstration:
<?
$a="a b c";
echo "Sample 1 ";
echo $b=str_replace("b", "_", $a);
echo "\n";
echo "Sample 2 ";
echo str_replace("_", "BAD", $b);
echo "\n";
$find=array('/b/', "/_/");
$replace=array('_', 'BAD');
echo "Sample 3 ";
echo preg_replace($find, $replace, $a);
echo "\n";
?>
This prints out
Sample 1 a _ c
Sample 2 a BAD c
Sample 3 a BAD c
Since I'd expected preg_replace to operate in one pass, I'd expected the output to be:
Sample 1 a _ c
Sample 2 a BAD c
Sample 3 a b c
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52135
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52135&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52135&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52135&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52135&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52135&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52135&report=yes
Search -- http://master.php.net/manage/user-notes.php