PHP多字节的str_replace? [英] PHP Multi Byte str_replace?
问题描述
我正在尝试在PHP中进行重音字符替换,但得到的结果很时髦,我的猜测是因为我使用的是UTF-8字符串,而str_replace无法正确处理多字节字符串..
I'm trying to do accented character replacement in PHP but get funky results, my guess being because i'm using a UTF-8 string and str_replace can't properly handle multi-byte strings..
$accents_search = array('á','à','â','ã','ª','ä','å','Á','À','Â','Ã','Ä','é','è',
'ê','ë','É','È','Ê','Ë','í','ì','î','ï','Í','Ì','Î','Ï','œ','ò','ó','ô','õ','º','ø',
'Ø','Ó','Ò','Ô','Õ','ú','ù','û','Ú','Ù','Û','ç','Ç','Ñ','ñ');
$accents_replace = array('a','a','a','a','a','a','a','A','A','A','A','A','e','e',
'e','e','E','E','E','E','i','i','i','i','I','I','I','I','oe','o','o','o','o','o','o',
'O','O','O','O','O','u','u','u','U','U','U','c','C','N','n');
$str = str_replace($accents_search, $accents_replace, $str);
我得到的结果:
Ørjan Nilsen -> �orjan Nilsen
预期结果:
Ørjan Nilsen -> Orjan Nilsen
我已经将内部字符处理程序设置为UTF-8(根据mb_internal_encoding()),而且$ str的值也是UTF-8,所以据我所知,所有涉及的字符串都是UTF -8. str_replace()是否可以检测到字符集并正确使用它们?
I've got my internal character handler set to UTF-8 (according to mb_internal_encoding()), also the value of $str is UTF-8, so from what I can tell, all the strings involved are UTF-8. Does str_replace() detect char sets and use them properly?
推荐答案
好像没有替换字符串,因为您的输入编码和文件编码不匹配.
Looks like the string was not replaced because your input encoding and the file encoding mismatch.
这篇关于PHP多字节的str_replace?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!