PHP iconv greek/cyrillic transliteration does not work

↘锁芯ラ 提交于 2019-12-22 09:07:42

问题


i have the following test code:

setlocale(LC_ALL, 'en_US.UTF8');
function t($text)
{
    echo "$text\n";
    echo "encoding: ", mb_detect_encoding($text), "\n";

    // transliterate
    $text = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $text);
    echo "iconv: ", $text, "\n";
}

// Latvian alphabet
t('AĀBCČDEĒFGĢHIĪJKĶLĻMNŅOPRSŠTUŪVZŽ aābcčdeēfgģhiījkķlļmnņoprsštuūvzž');
// Greek alphabet
t('ΑαΒβΓγΔδΕεΖζΗηΘθΙιΚκΜμΝνΞξΟοΠπΡρΣσςΤτΥυΦφΧχΨψΩω');
// Cyrillic alphabet + some rarer versions
t('АБВГДЕЖЅЗИІКЛМНОПҀРСТѸФХѠЦЧШЩЪꙐЬѢꙖѤЮѦѪѨѬѮѰѲѴ абвгдеёжзийклмнопрстуфхцчшщъыьэюя');

and its output:

AĀBCČDEĒFGĢHIĪJKĶLĻMNŅOPRSŠTUŪVZŽ aābcčdeēfgģhiījkķlļmnņoprsštuūvzž
encoding: UTF-8
iconv: AABCCDEEFGGHIIJKKLLMNNOPRSSTUUVZZ aabccdeefgghiijkkllmnnoprsstuuvzz

ΑαΒβΓγΔδΕεΖζΗηΘθΙιΚκΜμΝνΞξΟοΠπΡρΣσςΤτΥυΦφΧχΨψΩω
encoding: UTF-8
iconv: 

АБВГДЕЖЅЗИІКЛМНОПҀРСТѸФХѠЦЧШЩЪꙐЬѢꙖѤЮѦѪѨѬѮѰѲѴ абвгдеёжзийклмнопрстуфхцчшщъыьэюя
encoding: UTF-8
iconv: 

it essentially IGNOREs all greek and cyrillic characters. why?

i have tested on two environments, where php -i | egrep "iconv (implementation|library)" outputs either:

iconv implementation => libiconv
iconv library version => 1.11

or:

iconv implementation => libiconv
iconv library version => 1.13

i have also tried setting ivonv internal encoding to UTF-8, adding/removing the setlocale function, but all of no avail. iconv seems to recognise only latin and derived-from-latin characters.

UPDATE: It must be a problem with iconv as terminal command echo 'ΑαΒβΓγΔδ' | iconv -f utf-8 -t ASCII//TRANSLIT produces an error iconv: (stdin):1:0: cannot convert, while echo 'āēī' | iconv -f utf-8 -t ASCII//TRANSLIT works and outputs aei, as expected.

iconv --version outputs iconv (GNU libiconv 1.14) (besides the copyright information).


回答1:


use ASCII//IGNORE//TRANSLIT

The iconv() stopped at the first illegar char, cutting off the string right there, which is the default behaviour of iconv(), so it did not respect the //IGNORE switch after the //TRANSLIT



来源:https://stackoverflow.com/questions/8399191/php-iconv-greek-cyrillic-transliteration-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!