开发者

How to Convert Special Chars to Standard Chars?

开发者 https://www.devze.com 2023-03-09 16:58 出处:网络
I\'m looking for way to convert chars like āžšķūņrūķīš to azskunrukis. In other words, to replace ā with a, ž with z and so. Is t开发者_如何学Pythonhere anything built-in, or I should crea

I'm looking for way to convert chars like āžšķūņrūķīš to azskunrukis. In other words, to replace ā with a, ž with z and so. Is t开发者_如何学Pythonhere anything built-in, or I should create my own "library" of from-to symbols?


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


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".


As an alternative to iconv, you could check out the Normalize functions of the intl extension (if available).

0

精彩评论

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

关注公众号