I'm looking for a UTF-8 compatible strtr for PHP.
The utf8_encode() function is an inbuilt function in PHP which is used to encode an ISO-8859-1 string to UTF-8.
The strtr() function translates certain characters in a string. Note: If the from and to parameters are different in length, both will be formatted to the length of the shortest.
function strtr_utf8($str, $from, $to) {
$keys = array();
$values = array();
preg_match_all('/./u', $from, $keys);
preg_match_all('/./u', $to, $values);
$mapping = array_combine($keys[0], $values[0]);
return strtr($str, $mapping);
}
function strtr_utf8($str, $from, $to) { $keys = array(); $values = array(); if(!is_array($from)) { preg_match_all('/./u', $from, $keys); preg_match_all('/./u', $to, $values); $mapping = array_combine($keys[0], $values[0]); }else $mapping=$from; return strtr($str, $mapping); }
I slightly edited the joeforker's function to return back the functionality of using second parameter as array for replace_pairs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With