問題描述
我正在嘗試在 PHP 中進(jìn)行重音字符替換,但得到的結(jié)果很奇怪,我猜是因為我使用的是 UTF-8 字符串,而 str_replace 無法正確處理多字節(jié)字符串..
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('á','à','a','?','a','?','?','á','à','?','?','?','é','è',
'ê','?','é','è','ê','?','í','ì','?','?','í','ì','?','?','?','ò','ó','?','?','o','?',
'?','ó','ò','?','?','ú','ù','?','ú','ù','?','?','?','?','?');
$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);
我得到的結(jié)果:
?rjan Nilsen -> ?orjan Nilsen
預(yù)期結(jié)果:
?rjan Nilsen -> Orjan Nilsen
我的內(nèi)部字符處理程序設(shè)置為 UTF-8(根據(jù) mb_internal_encoding()),$str 的值也是 UTF-8,所以據(jù)我所知,所有涉及的字符串都是 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.
這篇關(guān)于PHP 多字節(jié) str_replace?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!