【发布时间】:2012-11-16 20:49:40
【问题描述】:
有什么好的解决方案可以很好地进行这种音译吗?
我尝试过使用iconv(),但它很烦人,而且它的行为不像人们预期的那样。
- 使用
//TRANSLIT将尝试替换它可以替换的内容,将所有不可转换的内容保留为“?” - 使用
//IGNORE不会离开“?”在文本中,但也不会音译,并且在找到不可转换的字符时也会引发E_NOTICE,因此您必须将 iconv 与@错误抑制器一起使用 - 使用
//IGNORE//TRANSLIT(正如一些人在PHP 论坛中建议的那样)实际上与//IGNORE相同(我自己在php 版本5.3.2 和5.3.13 上尝试过) - 同样使用
//TRANSLIT//IGNORE与//TRANSLIT相同
它还使用当前的语言环境设置来音译。
警告 - 后面有大量文字和代码!
这里有一些例子:
$text = 'Regular ascii text + čćžšđ + äöüß + éĕěėëȩ + æø€ + $ + ¶ + @';
echo '<br />original: ' . $text;
echo '<br />regular: ' . iconv("UTF-8", "ASCII//TRANSLIT", $text);
//> regular: Regular ascii text + ????? + ???ss + ?????? + ae?EUR + $ + ? + @
setlocale(LC_ALL, 'en_GB');
echo '<br />en_GB: ' . iconv("UTF-8", "ASCII//TRANSLIT", $text);
//> en_GB: Regular ascii text + cczs? + aouss + eeeeee + ae?EUR + $ + ? + @
setlocale(LC_ALL, 'en_GB.UTF8'); // will this work?
echo '<br />en_GB.UTF8: ' . iconv("UTF-8", "ASCII//TRANSLIT", $text);
//> en_GB.UTF8: Regular ascii text + cczs? + aouss + eeeeee + ae?EUR + $ + ? + @
好吧,确实转换了 č ć š ä ö ü ß é ĕ ě ė ë ȩ 和 æ,但为什么不转换 đ 和 ø?
// now specific locales
setlocale(LC_ALL, 'hr_Hr'); // this should fix croatian đ, right?
echo '<br />hr_Hr: ' . iconv("UTF-8", "ASCII//TRANSLIT", $text);
// wrong > hr_Hr: Regular ascii text + cczs? + aouss + eeeeee + ae?EUR + $ + ? + @
setlocale(LC_ALL, 'sv_SE'); // so this will fix swedish ø?
echo '<br />sv_SE: ' . iconv("UTF-8", "ASCII//TRANSLIT", $text);
// will not > sv_SE: Regular ascii text + cczs? + aouss + eeeeee + ae?EUR + $ + ? + @
//this is interesting
setlocale(LC_ALL, 'de_DE');
echo '<br />de_DE: ' . iconv("UTF-8", "ASCII//TRANSLIT", $text);
//> de_DE: Regular ascii text + cczs? + aeoeuess + eeeeee + ae?EUR + $ + ? + @
// actually this is what any german would expect since ä ö ü really is same as ae oe ue
让我们试试//IGNORE:
echo '<br />ignore: ' . iconv("UTF-8", "ASCII//IGNORE", $text);
//> ignore: Regular ascii text + + + + + $ + + @
//+ E_NOTICE: "Notice: iconv(): Detected an illegal character in input string in /var/www/test.server.web/index.php on line 49"
// with translit?
echo '<br />ignore/translit: ' . iconv("UTF-8", "ASCII//IGNORE//TRANSLIT", $text);
//same as ignore only> ignore/translit: Regular ascii text + + + + + $ + + @
//+ E_NOTICE: "Notice: iconv(): Detected an illegal character in input string in /var/www/test.server.web/index.php on line 54"
// translit/ignore?
echo '<br />translit/ignore: ' . iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $text);
//same as translit only> translit/ignore: Regular ascii text + cczs? + aouss + eeeeee + ae?EUR + $ + ? + @
使用solution of this guy 也无法正常工作:Regular ascii text + YYYYY + aous + eYYYeY + aoY + $ + � + @
即使使用 PECL intl Normalizer 类(即使您的 PHP > 5.3.0 也不总是可用,因为 ICU 包 intl 使用可能对 PHP 不可用,即在某些托管服务器上)会产生错误的结果:
echo '<br />normalize: ' .preg_replace('/\p{Mn}/u', '', Normalizer::normalize($text, Normalizer::FORM_KD));
//>normalize: Regular ascii text + cczsđ + aouß + eeeeee + æø€ + $ + ¶ + @
那么有没有其他方法可以做到这一点,或者唯一正确的做法是做preg_replace() 或str_replace() 并自己定义音译表?
// 附录: 我在 2008 年的 ZF wiki 辩论中发现了关于 proposal for Zend_Filter_Transliterate 的问题,但由于在某些语言中无法转换(即中文),因此项目被放弃,但对于任何基于拉丁语和西里尔语的语言 IMO 仍然应该存在此选项。
【问题讨论】:
-
为什么要将 utf8 转换为 ascii? uft8 是有史以来最伟大的事情......
-
@OptimusCrime : 转化为url,转化为html的id属性值,匹配相似词
-
嗯,对于 url,您可以简单地使用正则表达式替换除空格、a-z、A-Z 和数字之外的所有内容。特殊字符对于这种用法不是一件好事。 html 属性也是如此。
-
@OptimusCrime čćžšđäöüøñæé.... 不是特殊字符,将它们转换为 ascii 然后简单地从 url 字符串中删除它们会有很大的不同
-
对于那些询问为什么 OP 需要这个的人 - 这是一个很好的例子 - 在音乐搜索网站中,您可能搜索“fur elise”并期望找到“Für Elise”(带有u 上的变音符号)。我们需要一种方法来翻译变音符号并将其转换为 u - 这是我们人类在搜索时输入的内容 - 我添加了一个包含所有字段 TRANSLIT 的关键字字段,以便用户可以对其进行搜索。
标签: php utf-8 ascii iconv transliteration