note 106282 added to function.strtr

From: Date: Mon, 24 Oct 2011 17:02:25 +0000
Subject: note 106282 added to function.strtr
Groups: php.notes 
Request: Send a blank email to php-notes+get-184110@lists.php.net to get a copy of this message
I found that this approach is often faster than strtr() and won't change the same thing in your string twice (as opposed to str_replace(), which will overwrite things in the order of the array you feed it with): <?php function replace ($text, $replace) { $keys = array_keys($replace); $length = array_combine($keys, array_map('strlen', $keys)); arsort($length); $array[] = $text; $count = 1; reset($length); while ($key = key($length)) { if (strpos($text, $key) !== false) { for ($i = 0; $i < $count; $i += 2) { if (($pos = strpos($array[$i], $key)) === false) continue; array_splice($array, $i, 1, array(substr($array[$i], 0, $pos), $replace[$key], substr($array[$i], $pos + strlen($key)))); $count += 2; } } next($length); } return implode($array); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 87.211.81.93 ---- Manual Page -- http://www.php.net/manual/en/function.strtr.php Edit -- https://master.php.net/note/edit/106282 Del: integrated -- https://master.php.net/note/delete/106282/integrated Del: useless -- https://master.php.net/note/delete/106282/useless Del: bad code -- https://master.php.net/note/delete/106282/bad+code Del: spam -- https://master.php.net/note/delete/106282/spam Del: non-english -- https://master.php.net/note/delete/106282/non-english Del: in docs -- https://master.php.net/note/delete/106282/in+docs Del: other reasons-- https://master.php.net/note/delete/106282 Reject -- https://master.php.net/note/reject/106282 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184110) next »