问题
I'm looking for way to convert chars like āžšķūņrūķīš to azskunrukis. In other words, to replace ā with a, ž with z and so. Is there anything built-in, or I should create my own "library" of from-to symbols?
回答1:
Take a look at iconv's transliteration capabilities:
<?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 '
Your example text can be tranliterated using:
$translit = iconv('UTF-8', 'US-ASCII//TRANSLIT', 'āžšķūņrūķīš');
Here's an example with the text you provided: http://ideone.com/MJHvf
回答2:
I'm not sure of any functions that do this directly, but there are some implementations of translation tables that do something like that in the comments on strtr's documentation page. They end up using a table that directly translates each character to its equivalent, i.e. "ž" => "z".
回答3:
As an alternative to iconv
, you could check out the Normalize functions of the intl extension (if available).
来源:https://stackoverflow.com/questions/6206132/how-to-convert-special-chars-to-standard-chars