如何将特殊字符转换为标准字符? [英] How to Convert Special Chars to Standard Chars?

查看:158
本文介绍了如何将特殊字符转换为标准字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找将āžšķūņrūķīš这样的字符转换为 azskunrukis 。换句话说,要用 a 替换āž z 等等。有什么内置的,还是应该创建自己的库的从符号?

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?

推荐答案

iconv的音译功能

<?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:

原文:这是欧元符号'€'。

TRANSLIT:这是欧元符号'EUR'。

IGNORE:这是欧元符号。

平原:

注意:iconv():在第7行的.\iconv-example.php中检测到输入字符串中的非法字符

这是欧元符号'

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ūķīš');

以下是您提供的文字的示例: http://ideone.com/MJHvf

Here's an example with the text you provided: http://ideone.com/MJHvf

这篇关于如何将特殊字符转换为标准字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆