Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

php - Convert accented characters to their plain ascii equivalents

I have to convert french characters into english on my php. I've used the following code:

iconv("utf-8", "ascii//TRANSLIT", $string);

But the result for ??? was "E"E"E.

I don't need that double quote and other extra characters - I want to show an output like EEE. Is there any other method to convert french to english? Can you help me to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The PHP Manual iconv Intro has a warning:

Note that the iconv function on some systems may not work as you expect. In such case, it'd be a good idea to install the GNU libiconv library. It will most likely end up with more consistent results.

But if accented characters are the only issue then you could use a dirty strtr (partially from strtr comments):

$string = '? à ì ? í ? ? ? ? ? ò è ó é ? ê ? ? ê ù ? ú ? ? ? ü ? Y ? a ? ? ? ?';

$normalizeChars = array(
    '?'=>'S', '?'=>'s', 'D'=>'Dj','?'=>'Z', '?'=>'z', 'à'=>'A', 'á'=>'A', '?'=>'A', '?'=>'A', '?'=>'A',
    '?'=>'A', '?'=>'A', '?'=>'C', 'è'=>'E', 'é'=>'E', 'ê'=>'E', '?'=>'E', 'ì'=>'I', 'í'=>'I', '?'=>'I',
    '?'=>'I', '?'=>'N', '?'=>'N', 'ò'=>'O', 'ó'=>'O', '?'=>'O', '?'=>'O', '?'=>'O', '?'=>'O', 'ù'=>'U', 'ú'=>'U',
    '?'=>'U', 'ü'=>'U', 'Y'=>'Y', 'T'=>'B', '?'=>'Ss','à'=>'a', 'á'=>'a', 'a'=>'a', '?'=>'a', '?'=>'a',
    '?'=>'a', '?'=>'a', '?'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', '?'=>'e', 'ì'=>'i', 'í'=>'i', '?'=>'i',
    '?'=>'i', 'e'=>'o', '?'=>'n', 'ń'=>'n', 'ò'=>'o', 'ó'=>'o', '?'=>'o', '?'=>'o', '?'=>'o', '?'=>'o', 'ù'=>'u',
    'ú'=>'u', '?'=>'u', 'ü'=>'u', 'y'=>'y', 'y'=>'y', 't'=>'b', '?'=>'y', '?'=>'f',
    '?'=>'a', '?'=>'i', 'a'=>'a', '?'=>'s', '?'=>'t', '?'=>'A', '?'=>'I', '?'=>'A', '?'=>'S', '?'=>'T',
);

//Output: E A I A I A I A I C O E O E O E O O e U e U i U i U o Y o a u a y c
echo strtr($string, $normalizeChars);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...