str_replace versus strtr
Posted: Thu Jan 22, 2009 9:54 pm
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);
?>