note 91536 added to function.str-replace
| From: | AlbertoLepe at osu1 dot php dot net | Date: | Tue, 16 Jun 2009 02:44:50 +0000 |
| Subject: | note 91536 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156905@lists.php.net to get a copy of this message | ||
Be careful when replacing characters (or repeated patterns in the FROM and TO arrays):
For example:
$arrFrom = array("1","2","3","B");
$arrTo = array("A","B","C","D");
$word = "ZBB2";
echo str_replace($arrFrom, $arrTo, $word);
I would expect as result: "ZDDB"
However, this return: "ZDDD"
(Because B = D according to our array)
To make this work, use "strtr" instead:
$arr = array("1" => "A","2" => "B","3" =>
"C","B" => "D");
$word = "ZBB2";
echo strtr($word,$arr);
This returns: "ZDDB"
----
Server IP: 69.147.83.197
Probable Submitter: 211.6.179.194
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/91536
Del: integrated -- https://master.php.net/note/delete/91536/integrated
Del: useless -- https://master.php.net/note/delete/91536/useless
Del: bad code -- https://master.php.net/note/delete/91536/bad+code
Del: spam -- https://master.php.net/note/delete/91536/spam
Del: non-english -- https://master.php.net/note/delete/91536/non-english
Del: in docs -- https://master.php.net/note/delete/91536/in+docs
Del: other reasons-- https://master.php.net/note/delete/91536
Reject -- https://master.php.net/note/reject/91536
Search -- https://master.php.net/manage/user-notes.php