开发者

Why doesn't translit work?

setlocale(LC_ALL, 开发者_运维百科'en_US.UTF8');
$string= 'ṃỹṛèşưḿĕ';
echo iconv('UTF-8', 'ASCII//TRANSLIT', $string);

makes error...

should print: myresume


It depends on the iconv library.

In Ubuntu 10.10, I get this:

$ php -i | egrep "iconv (implementation|library)"
iconv implementation => glibc
iconv library version => 2.12.1
$ php a.php 
myresume

But on another machine using the GNU iconv:

iconv implementation => libiconv
iconv library version => 1.11
# php a.php 
Notice: iconv(): Unknown error (88) in /tmp/root/a.php on line 5

The transliteration done by iconv is not consistent across implementations. For instance, the glibc implementation transliterates é into e, but libiconv transliterates it into 'e.

Until we have support for ICU transliterators in PHP (due for the next version), there won't be a realiable way to reliable do these transformation (though if you only want to remove marks, there are other solutions). In the development version of PHP, with the intl extension, it's possible to do this:

<?php
$t = Transliterator::create("latin; NFKD; [^\u0000-\u007E] Remove; NFC");
echo $t->transliterate('Ναδάλης ṃỹṛèşưḿĕ');

which gives

Nadales myresume
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜