菜单

iconv utf-8 iso-8859-1

2011年08月27日 - php

Example #1 iconv() example
<?php
$text = “This is the Euro symbol ‘€’.”;

echo ‘Original : ‘, $text, PHP_EOL;
echo ‘TRANSLIT : ‘, iconv(“UTF-8”, “ISO-8859-1//TRANSLIT”, $text), PHP_EOL;
echo ‘IGNORE   : ‘, iconv(“UTF-8”, “ISO-8859-1//IGNORE”, $text), PHP_EOL;
echo ‘Plain    : ‘, iconv(“UTF-8”, “ISO-8859-1”, $text), PHP_EOL;

?>

The above example will output something similar to:

Original : This is the Euro symbol ‘€’.
TRANSLIT : This is the Euro symbol ‘EUR’.
IGNORE   : This is the Euro symbol ”.
Plain    :
Notice: iconv(): Detected an illegal character in input string in .\iconv-example.php on line 7
This is the Euro symbol ‘

 

I had a situation where I needed some characters transliterated, but the others ignored (for weird diacritics like ayn or hamza).

Adding //TRANSLIT//IGNORE seemed to do the trick for me.

It transliterates everything that is able to be transliterated, but then throws out stuff that can’t be.

So:

<?php
$string = “ʿABBĀSĀBĀD”;

echo iconv(‘UTF-8’, ‘ISO-8859-1//TRANSLIT’, $string);
// output: [nothing, and you get a notice]

echo iconv(‘UTF-8’, ‘ISO-8859-1//IGNORE’, $string);
// output: ABBSBD

echo iconv(‘UTF-8’, ‘ISO-8859-1//TRANSLIT//IGNORE’, $string);
// output: ABBASABAD
// Yay! That’s what I wanted!

?>

发表评论

电子邮件地址不会被公开。 必填项已用*标注