我正在尝试使用 iconv 从 PHP 中的某些文本中删除所有重音符号等。它会去除重音符号,但有时会添加额外的字符,例如:
“Théâtre”
输出为 “Th'e^atre”
而我想要 “Theatre”
这是我的代码:
setlocale(LC_ALL, "fr_FR.utf8");
$text = "Théâtre";
$text = iconv("utf-8","ascii//TRANSLIT//IGNORE",$text);
echo $text;
结果:Th'e^atre
最佳答案
此错误可能是由于您的服务器上的 iconv()
实现错误造成的(glibc 而不是 libiconv,您可以检查一下在 phpinfo()
的输出中)。请参阅here了解更多详情。
您也可以尝试:
$text = "Théâtre";
$text = iconv("utf-8","ascii//TRANSLIT",$text); //$text = "Th'e^atre"
//Replace any character not in alphabet by an empty substring.
$text = preg_replace("#[^a-zA-z]#", "", $text); //$text = "Theatre"
echo $text;
关于PHP Iconv 添加额外字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47501779/