str_replace versus strtr

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

str_replace versus strtr

Post by JAB Creations »

Thanks to tasairis's suggestion I ended up achieving my goal of converting HTML entities to numeric entities. However I'm wondering about the difference between str_replace and strtr. What are some examples of where one is better then the other, preferably for both please?

Code: Select all

<?php
function entity_convert($convert)
{
 $entity['&'] = '&';
 $entity['<'] = '<';
 $entity['>'] = '>';
 
 foreach($entity as $key=>$value) {$convert=str_replace($key,$value,$convert);}
 return $convert;
}
 
$s = "<&><&>";
echo entity_convert($s);
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: str_replace versus strtr

Post by requinix »

I think you'll find strtr will be easier than str_replace in your case ;)


strtr translates each character in one string to a character in another string.

Code: Select all

echo strtr("alphabet", "abc", "xyz"); // xlphxyet
str_replace can replace an array of strings with another string in the second array. To do the same as the above code you'd need

Code: Select all

str_replace(array("a", "b", "c"), array("x", "y", "z"), "alphabet")
PHP extended strtr to be a bit like str_replace. Instead of two strings to replace individual character you can pass an array where the key is a search string and the value is the replacement.

Code: Select all

$array = array("find" => "replace");
$string = "Example how strtr and str_replace can do a find/replace with strings";
 
echo strtr($string, $array); // Example... do a replace/replace with strings
 
// or with str_replace:
$find = array_keys($array);
$replace = array_values($array); // you can just use $array, actually
$string = str_replace($find, $replace, $string);
echo $string;
As for why they didn't extend str_replace to use one array instead of two, I don't know.


So basically:
- Replacing one character with another (like a mapping): strtr
- The search/replace strings are in separate arrays: str_replace
- The search/replace strings are in one array like search=>replace: strtr
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: str_replace versus strtr

Post by requinix »

(Because that post is long enough already...)

There is one difference between the two though:

Code: Select all

$array = array("long" => "short", "short" => "shorter");
$find = array_keys($array); $replace = array_values($array);
 
$string = "Find the longer word and replace with something shorter";
 
echo strtr($string, $array);
// Find the shorter word and replace with something shorterer
// "longer" => "shorter"
 
echo str_replace($find, $replace, $string);
// Find the shorterer word and replace with something shorterer
// "longer" => "shorter" => "shorterer"
str_replace will do replacements without a second thought. This might mean that a word gets replaced once, then part of that gets replaced again.
strtr doesn't have that - it looks for the longest match first, then the next longest, and so on.
Post Reply