将外文字符更改为通常的对等字符

时间:2022-08-14 19:24:57

I am using php and I was wondering if there was a predefined way to convert foreign characters to their non-foreign alternatives.

我正在使用php,我想知道是否有一种预定义的方法可以将外置字符转换为非外置字符。

Characters such as ê, ë, é all resulting to 'e'.
I'm looking for a function that would take a string and return it without the special characters.
Any ideas would be greatly appreciated!

像e, e, e这样的字符都产生了e。我在寻找一个函数,它可以取一个字符串并返回它而不需要特殊的字符。任何想法都将非常感谢!

7 个解决方案

#1


73  

After failing to find suitable convertors I created my own collection that suits my needs including my favorite Cyrillic conversion that by default has numerous variations.

在没有找到合适的转换器之后,我创建了适合我需要的我自己的集合,包括我最喜欢的Cyrillic转换,默认情况下它有很多变化。

function transliterateString($txt) {
    $transliterationTable = array('á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', 'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C', 'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh', 'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E', 'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E', 'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G', 'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H', 'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I', 'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L', 'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N', 'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O', 'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O', 'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE', 'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R', 'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S', 'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't', 'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u', 'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u', 'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u', 'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w', 'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y', 'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z', 'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'a', 'б' => 'b', 'Б' => 'b', 'в' => 'v', 'В' => 'v', 'г' => 'g', 'Г' => 'g', 'д' => 'd', 'Д' => 'd', 'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'zh', 'з' => 'z', 'З' => 'z', 'и' => 'i', 'И' => 'i', 'й' => 'j', 'Й' => 'j', 'к' => 'k', 'К' => 'k', 'л' => 'l', 'Л' => 'l', 'м' => 'm', 'М' => 'm', 'н' => 'n', 'Н' => 'n', 'о' => 'o', 'О' => 'o', 'п' => 'p', 'П' => 'p', 'р' => 'r', 'Р' => 'r', 'с' => 's', 'С' => 's', 'т' => 't', 'Т' => 't', 'у' => 'u', 'У' => 'u', 'ф' => 'f', 'Ф' => 'f', 'х' => 'h', 'Х' => 'h', 'ц' => 'c', 'Ц' => 'c', 'ч' => 'ch', 'Ч' => 'ch', 'ш' => 'sh', 'Ш' => 'sh', 'щ' => 'sch', 'Щ' => 'sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'y', 'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja');
    return str_replace(array_keys($transliterationTable), array_values($transliterationTable), $txt);
}

#2


8  

I coded this function which uses the HTML entities translation table built-in into PHP to romanize chars:

我编写了这个函数,它使用内置到PHP中的HTML实体翻译表来罗马化chars:

function Unaccent($string)
{
    if (strpos($string = htmlentities($string, ENT_QUOTES, 'UTF-8'), '&') !== false)
    {
        $string = html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~i', '$1', $string), ENT_QUOTES, 'UTF-8');
    }

    return $string;
}

It works by applying htmlentities() and then removing common entities suffixes, a simple example:

它通过应用htmlentities(),然后删除公共实体后缀来工作,这是一个简单的示例:

 - ã = ã -> a
 - Ã = Ã -> A
 - õ = õ -> o
 - Õ = Õ -> O
 - æ = æ  -> ae
 - Æ = Æ  -> AE

Beware that for this to work properly your files need to be encoded in UTF-8 (no BOM obviously).

要注意,为了使其正常工作,您的文件需要用UTF-8编码(显然不是BOM)。

See also my other answer for another example.

再看另一个例子。

#3


7  

Try iconv() http://www.php.net/manual/en/function.iconv.php with the //TRANSLIT option, or

使用//TRANSLIT选项(或)来尝试iconv() http://www.php.net/manual/en/function.iconv.php。

recode_string() http://www.php.net/manual/en/function.recode-string.php, or

recode_string()http://www.php.net/manual/en/function.recode-string.php,或

mb_convert_encoding() http://www.php.net/manual/en/function.mb-convert-encoding.php

mb_convert_encoding http://www.php.net/manual/en/function.mb-convert-encoding.php()

#4


7  

My first recommendation is the iconv function. Namely because it's built into PHP, so doesn't require any external or 3rd party libraries. In addition, it's a function that's designed to do precisely what you are trying to accomplish (accept on character set as input, and output an alternate character set, specifically going from UTF-8 to ASCII). Below is an example of how to call this function:

我的第一个建议是iconv函数。也就是说,因为它内置在PHP中,所以不需要任何外部或第三方库。此外,它是一个设计用来精确完成您想要完成的任务的函数(接受字符集作为输入,并输出另一个字符集,特别是从UTF-8到ASCII)。下面是一个如何调用这个函数的例子:

$clean_ascii_output = iconv('UTF-8', 'ASCII//TRANSLIT', $utf8_input);

More information about the specifics of this PHP function can be found here: http://php.net/manual/en/function.iconv.php

关于这个PHP函数的详细信息可以在这里找到:http://php.net/manual/en/function.iconv.php

Note: The iconv function accepts string inputs, so you'll want to iterate over data, and parse it such that you are passing in a string input.

注意:iconv函数接受字符串输入,所以您需要对数据进行迭代,并对其进行解析,以便传递字符串输入。

#5


1  

I hope this will be useful for anybody: https://github.com/infralabs/DiacriticsRemovePHP

我希望这对任何人都有用:https://github.com/infralabs/DiacriticsRemovePHP

This class removes diacritics from strings containing Latin-1 Supplement, Latin Extended-A and Latin Extended-B special characters.

这个类从包含拉丁-1补充、拉丁- a扩展和拉丁- b特殊字符的字符串中删除变音符。

usage:

用法:

$specialCharacters = "";
$specialCharacters .= "Latin-1 Supplement".PHP_EOL;
$specialCharacters .= "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ".PHP_EOL;
$specialCharacters .= "Latin Extended-A".PHP_EOL;
$specialCharacters .= "ĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽžſ".PHP_EOL;
$specialCharacters .= "Latin Extended-B".PHP_EOL;
$specialCharacters .= "ƒǺǻǼǽǾǿ".PHP_EOL;
$specialCharacters .= "Latin Extended Additional".PHP_EOL;
$specialCharacters .= "ẀẁẂẃẄẅỲỳ".PHP_EOL;

print "<pre>";
print removeDiacritics($specialCharacters).PHP_EOL;
print "</pre>";

source:

来源:

Latin-1 Supplement

latin - 1补充

ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ

AAAAAAÆCEEEEIIIIÐNOOOOO×ØUUUUYÞßaaaaaaæceeeeiiiiðnooooo÷øuuuuyþy

Latin Extended-A

拉丁延长了

ĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽžſ

ĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőœœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷÿŹźŻżŽžſ

Latin Extended-B

拉丁Extended-B

ƒǺǻǼǽǾǿ

ƒǺǻǼǽǾǿ

Latin Extended Additional

拉丁扩展附加

ẀẁẂẃẄẅỲỳ

ẀẁẂẃẄẅỲỳ

result:

结果:

Latin-1 Supplement

latin - 1补充

AAAAAAAECEEEEIIIIDNOOOOO×OUUUUYTHssaaaaaaaeceeeeiiiidnooooo÷ouuuuythy

AAAAAAAECEEEEIIIIDNOOOOO×OUUUUYTHssaaaaaaaeceeeeiiiidnooooo÷ouuuuythy

Latin Extended-A

拉丁延长了

AaAaAaCcCcCcCcDdDdEeEeEeEeEeGgGgGgGgHhHhIiIiIiIiIiIJijJjKkĸLlLlLlLlLlNnNnNnnNnOoOoOoOEoeRrRrRrSsSsSsSsTtTtTtUuUuUuUuUuUuWwYyYZzZzZzs

AaAaAaCcCcCcCcDdDdEeEeEeEeEeGgGgGgGgHhHhIiIiIiIiIiIJijJjKkĸLlLlLlLlLlNnNnNnnNnOoOoOoOEoeRrRrRrSsSsSsSsTtTtTtUuUuUuUuUuUuWwYyYZzZzZzs

Latin Extended-B

拉丁Extended-B

fAaAEaeOo

fAaAEaeOo

Latin Extended Additional

拉丁扩展附加

WwWwWwYy

WwWwWwYy

#6


0  

The most generic way to solve this is to use Unicode Normalization as it works automatically on all accents - you don't have to prepare the list up front. I don't know if it's easily available in PHP, I have used it in C and Java. Essentially, you first transform the string so that all accented characters are represented by regular character plus so-called composing diacritical mark (a built-in or external library should provide this function), and then remove the composing diacritics (using a specialized library, using character properties the language provides or using some regular expression extensions).

解决这个问题的最通用的方法是使用Unicode标准化,因为它可以自动处理所有口音——您不必预先准备列表。我不知道PHP中是否容易使用,我在C和Java中使用过。从本质上说,你第一次转换的字符串,这样所有重音字符由普通字符+所谓组合可区别的标志(一个内置的或外部库应该提供这个函数),然后删除组合变音符号(使用专门的库,使用字符属性语言提供或使用一些正则表达式扩展)。

#7


-2  

I have spend days trying to figure this out, without a the use of a function. In codes like C#, Java its no problem at all.

我花了好几天的时间试图弄明白这一点,没有使用一个函数。在c#之类的代码中,Java根本没有问题。

But i am sorry to say that PHP just doesn't have a function like this.

但是很抱歉,PHP没有这样的函数。

To clear things up, (i think you want to achieve this):

为了把事情弄清楚(我想你想要达到这个目标):

For instance someone fills in a name, the name is

例如,有人填了一个名字,名字是。

übérhausen

And you want to use it in for example your url, maybe its an article name that directs to the article. in that case

你想要在你的url中使用它,可能是指向文章的文章名。在这种情况下

/articles/übérhausen/

Simply is not going to work, you want

你想要的是,根本行不通

/articles/uberhausen/

Where übérhausen is automaticly generated with a function or such in php to uberhausen.

其中uberhausen是用php中的一个或多个函数自动生成给uberhausen的。

If this is what you are looking for, then i am afraid php is not going to help out. I spend days trying to solve this matter without any luck. Sure Cellfex function works fine, but using a function to compare every letter with an array and find the proper replacement for it simply is to much prossesing for the server.

如果这是您要寻找的,那么我担心php不会提供帮助。我花了好几天的时间试图解决这个问题。当然,Cellfex函数工作得很好,但是使用一个函数将每个字母与一个数组进行比较,并找到正确的替换,仅仅是对服务器进行了大量的测试。

If someone has an solution to the problem, i will find you and hug you! haha Offcource you can encode and decode to and from html, its just that php doesnt have a proper function for it, iconv() comes close but will just delete the special chars.

如果有人能解决这个问题,我会找到你,拥抱你!你可以对html进行编码和解码,只是php没有合适的函数,iconv()很接近,但是会删除特殊的字符。

#1


73  

After failing to find suitable convertors I created my own collection that suits my needs including my favorite Cyrillic conversion that by default has numerous variations.

在没有找到合适的转换器之后,我创建了适合我需要的我自己的集合,包括我最喜欢的Cyrillic转换,默认情况下它有很多变化。

function transliterateString($txt) {
    $transliterationTable = array('á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', 'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C', 'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh', 'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E', 'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E', 'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G', 'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H', 'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I', 'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L', 'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N', 'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O', 'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O', 'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE', 'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R', 'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S', 'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't', 'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u', 'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u', 'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u', 'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w', 'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y', 'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z', 'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'a', 'б' => 'b', 'Б' => 'b', 'в' => 'v', 'В' => 'v', 'г' => 'g', 'Г' => 'g', 'д' => 'd', 'Д' => 'd', 'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'zh', 'з' => 'z', 'З' => 'z', 'и' => 'i', 'И' => 'i', 'й' => 'j', 'Й' => 'j', 'к' => 'k', 'К' => 'k', 'л' => 'l', 'Л' => 'l', 'м' => 'm', 'М' => 'm', 'н' => 'n', 'Н' => 'n', 'о' => 'o', 'О' => 'o', 'п' => 'p', 'П' => 'p', 'р' => 'r', 'Р' => 'r', 'с' => 's', 'С' => 's', 'т' => 't', 'Т' => 't', 'у' => 'u', 'У' => 'u', 'ф' => 'f', 'Ф' => 'f', 'х' => 'h', 'Х' => 'h', 'ц' => 'c', 'Ц' => 'c', 'ч' => 'ch', 'Ч' => 'ch', 'ш' => 'sh', 'Ш' => 'sh', 'щ' => 'sch', 'Щ' => 'sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'y', 'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja');
    return str_replace(array_keys($transliterationTable), array_values($transliterationTable), $txt);
}

#2


8  

I coded this function which uses the HTML entities translation table built-in into PHP to romanize chars:

我编写了这个函数,它使用内置到PHP中的HTML实体翻译表来罗马化chars:

function Unaccent($string)
{
    if (strpos($string = htmlentities($string, ENT_QUOTES, 'UTF-8'), '&') !== false)
    {
        $string = html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~i', '$1', $string), ENT_QUOTES, 'UTF-8');
    }

    return $string;
}

It works by applying htmlentities() and then removing common entities suffixes, a simple example:

它通过应用htmlentities(),然后删除公共实体后缀来工作,这是一个简单的示例:

 - ã = &atilde; -> a
 - Ã = &Atilde; -> A
 - õ = &otilde; -> o
 - Õ = &Otilde; -> O
 - æ = &aelig;  -> ae
 - Æ = &AElig;  -> AE

Beware that for this to work properly your files need to be encoded in UTF-8 (no BOM obviously).

要注意,为了使其正常工作,您的文件需要用UTF-8编码(显然不是BOM)。

See also my other answer for another example.

再看另一个例子。

#3


7  

Try iconv() http://www.php.net/manual/en/function.iconv.php with the //TRANSLIT option, or

使用//TRANSLIT选项(或)来尝试iconv() http://www.php.net/manual/en/function.iconv.php。

recode_string() http://www.php.net/manual/en/function.recode-string.php, or

recode_string()http://www.php.net/manual/en/function.recode-string.php,或

mb_convert_encoding() http://www.php.net/manual/en/function.mb-convert-encoding.php

mb_convert_encoding http://www.php.net/manual/en/function.mb-convert-encoding.php()

#4


7  

My first recommendation is the iconv function. Namely because it's built into PHP, so doesn't require any external or 3rd party libraries. In addition, it's a function that's designed to do precisely what you are trying to accomplish (accept on character set as input, and output an alternate character set, specifically going from UTF-8 to ASCII). Below is an example of how to call this function:

我的第一个建议是iconv函数。也就是说,因为它内置在PHP中,所以不需要任何外部或第三方库。此外,它是一个设计用来精确完成您想要完成的任务的函数(接受字符集作为输入,并输出另一个字符集,特别是从UTF-8到ASCII)。下面是一个如何调用这个函数的例子:

$clean_ascii_output = iconv('UTF-8', 'ASCII//TRANSLIT', $utf8_input);

More information about the specifics of this PHP function can be found here: http://php.net/manual/en/function.iconv.php

关于这个PHP函数的详细信息可以在这里找到:http://php.net/manual/en/function.iconv.php

Note: The iconv function accepts string inputs, so you'll want to iterate over data, and parse it such that you are passing in a string input.

注意:iconv函数接受字符串输入,所以您需要对数据进行迭代,并对其进行解析,以便传递字符串输入。

#5


1  

I hope this will be useful for anybody: https://github.com/infralabs/DiacriticsRemovePHP

我希望这对任何人都有用:https://github.com/infralabs/DiacriticsRemovePHP

This class removes diacritics from strings containing Latin-1 Supplement, Latin Extended-A and Latin Extended-B special characters.

这个类从包含拉丁-1补充、拉丁- a扩展和拉丁- b特殊字符的字符串中删除变音符。

usage:

用法:

$specialCharacters = "";
$specialCharacters .= "Latin-1 Supplement".PHP_EOL;
$specialCharacters .= "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ".PHP_EOL;
$specialCharacters .= "Latin Extended-A".PHP_EOL;
$specialCharacters .= "ĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽžſ".PHP_EOL;
$specialCharacters .= "Latin Extended-B".PHP_EOL;
$specialCharacters .= "ƒǺǻǼǽǾǿ".PHP_EOL;
$specialCharacters .= "Latin Extended Additional".PHP_EOL;
$specialCharacters .= "ẀẁẂẃẄẅỲỳ".PHP_EOL;

print "<pre>";
print removeDiacritics($specialCharacters).PHP_EOL;
print "</pre>";

source:

来源:

Latin-1 Supplement

latin - 1补充

ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ

AAAAAAÆCEEEEIIIIÐNOOOOO×ØUUUUYÞßaaaaaaæceeeeiiiiðnooooo÷øuuuuyþy

Latin Extended-A

拉丁延长了

ĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽžſ

ĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőœœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷÿŹźŻżŽžſ

Latin Extended-B

拉丁Extended-B

ƒǺǻǼǽǾǿ

ƒǺǻǼǽǾǿ

Latin Extended Additional

拉丁扩展附加

ẀẁẂẃẄẅỲỳ

ẀẁẂẃẄẅỲỳ

result:

结果:

Latin-1 Supplement

latin - 1补充

AAAAAAAECEEEEIIIIDNOOOOO×OUUUUYTHssaaaaaaaeceeeeiiiidnooooo÷ouuuuythy

AAAAAAAECEEEEIIIIDNOOOOO×OUUUUYTHssaaaaaaaeceeeeiiiidnooooo÷ouuuuythy

Latin Extended-A

拉丁延长了

AaAaAaCcCcCcCcDdDdEeEeEeEeEeGgGgGgGgHhHhIiIiIiIiIiIJijJjKkĸLlLlLlLlLlNnNnNnnNnOoOoOoOEoeRrRrRrSsSsSsSsTtTtTtUuUuUuUuUuUuWwYyYZzZzZzs

AaAaAaCcCcCcCcDdDdEeEeEeEeEeGgGgGgGgHhHhIiIiIiIiIiIJijJjKkĸLlLlLlLlLlNnNnNnnNnOoOoOoOEoeRrRrRrSsSsSsSsTtTtTtUuUuUuUuUuUuWwYyYZzZzZzs

Latin Extended-B

拉丁Extended-B

fAaAEaeOo

fAaAEaeOo

Latin Extended Additional

拉丁扩展附加

WwWwWwYy

WwWwWwYy

#6


0  

The most generic way to solve this is to use Unicode Normalization as it works automatically on all accents - you don't have to prepare the list up front. I don't know if it's easily available in PHP, I have used it in C and Java. Essentially, you first transform the string so that all accented characters are represented by regular character plus so-called composing diacritical mark (a built-in or external library should provide this function), and then remove the composing diacritics (using a specialized library, using character properties the language provides or using some regular expression extensions).

解决这个问题的最通用的方法是使用Unicode标准化,因为它可以自动处理所有口音——您不必预先准备列表。我不知道PHP中是否容易使用,我在C和Java中使用过。从本质上说,你第一次转换的字符串,这样所有重音字符由普通字符+所谓组合可区别的标志(一个内置的或外部库应该提供这个函数),然后删除组合变音符号(使用专门的库,使用字符属性语言提供或使用一些正则表达式扩展)。

#7


-2  

I have spend days trying to figure this out, without a the use of a function. In codes like C#, Java its no problem at all.

我花了好几天的时间试图弄明白这一点,没有使用一个函数。在c#之类的代码中,Java根本没有问题。

But i am sorry to say that PHP just doesn't have a function like this.

但是很抱歉,PHP没有这样的函数。

To clear things up, (i think you want to achieve this):

为了把事情弄清楚(我想你想要达到这个目标):

For instance someone fills in a name, the name is

例如,有人填了一个名字,名字是。

übérhausen

And you want to use it in for example your url, maybe its an article name that directs to the article. in that case

你想要在你的url中使用它,可能是指向文章的文章名。在这种情况下

/articles/übérhausen/

Simply is not going to work, you want

你想要的是,根本行不通

/articles/uberhausen/

Where übérhausen is automaticly generated with a function or such in php to uberhausen.

其中uberhausen是用php中的一个或多个函数自动生成给uberhausen的。

If this is what you are looking for, then i am afraid php is not going to help out. I spend days trying to solve this matter without any luck. Sure Cellfex function works fine, but using a function to compare every letter with an array and find the proper replacement for it simply is to much prossesing for the server.

如果这是您要寻找的,那么我担心php不会提供帮助。我花了好几天的时间试图解决这个问题。当然,Cellfex函数工作得很好,但是使用一个函数将每个字母与一个数组进行比较,并找到正确的替换,仅仅是对服务器进行了大量的测试。

If someone has an solution to the problem, i will find you and hug you! haha Offcource you can encode and decode to and from html, its just that php doesnt have a proper function for it, iconv() comes close but will just delete the special chars.

如果有人能解决这个问题,我会找到你,拥抱你!你可以对html进行编码和解码,只是php没有合适的函数,iconv()很接近,但是会删除特殊的字符。