将国家字符转换为PHP中的拉丁语

Jac*_*cuz 11 php string

我需要一些包含德语字符的字符串转换为拉丁语.例如

'Höhle' => 'Hohle'
Run Code Online (Sandbox Code Playgroud)

Mat*_*tič 9

最简单的方法是

echo transliterator_transliterate('Any-Latin; Latin-ASCII', "Höhle"); // returns Hohle
Run Code Online (Sandbox Code Playgroud)

  • @AurelijusRozenas 答案发布于 2013 年,我的评论来自 2016 年。那天 PHP 7.3 甚至还不存在。我的猜测是我在 PHP 5.6 上进行了测试,但它对我不起作用。另请注意,答案不提供适用的 PHP 版本。仅供参考,我并没有投反对票。刚刚留下评论。 (2认同)

sim*_*aun 7

它显然不包括每一个角色,但应该帮助一些更常见的角色:

<?php
/**
 * Replaces special characters in a string with their "non-special" counterpart.
 *
 * Useful for friendly URLs.
 *
 * @access public
 * @param string
 * @return string
 */
function convertAccentsAndSpecialToNormal($string) {
    $table = array(
        'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', '?'=>'A', '?'=>'A', '?'=>'A', 'Æ'=>'A', '?'=>'A',
        'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', '?'=>'a', '?'=>'a', '?'=>'a', 'æ'=>'a', '?'=>'a',

        'Þ'=>'B', 'þ'=>'b', 'ß'=>'Ss',

        'Ç'=>'C', '?'=>'C', '?'=>'C', '?'=>'C', '?'=>'C',
        'ç'=>'c', '?'=>'c', '?'=>'c', '?'=>'c', '?'=>'c',

        '?'=>'Dj', '?'=>'D',
        '?'=>'dj', '?'=>'d',

        'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', '?'=>'E', '?'=>'E', '?'=>'E', '?'=>'E',
        'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', '?'=>'e', '?'=>'e', '?'=>'e', '?'=>'e',

        '?'=>'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', '?'=>'i', '?'=>'i', '?'=>'i', '?'=>'i',

        '?'=>'J',
        '?'=>'j',

        '?'=>'K',
        '?'=>'k', '?'=>'k',

        '?'=>'L', '?'=>'L', '?'=>'L', '?'=>'L', '?'=>'L',
        '?'=>'l', '?'=>'l', '?'=>'l', '?'=>'l', '?'=>'l',

        'Ñ'=>'N', '?'=>'N', '?'=>'N', '?'=>'N', '?'=>'N',
        'ñ'=>'n', '?'=>'n', '?'=>'n', '?'=>'n', '?'=>'n', '?'=>'n',

        'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', '?'=>'O', '?'=>'O', '?'=>'O', 'Œ'=>'O',
        'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', '?'=>'o', '?'=>'o', '?'=>'o', 'œ'=>'o', 'ð'=>'o',

        '?'=>'R', '?'=>'R',
        '?'=>'r', '?'=>'r', '?'=>'r',

        'Š'=>'S', '?'=>'S', '?'=>'S', '?'=>'S',
        'š'=>'s', '?'=>'s', '?'=>'s', '?'=>'s',

        '?'=>'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',

        '?'=>'W', '?'=>'W', '?'=>'W', '?'=>'W',
        '?'=>'w', '?'=>'w', '?'=>'w', '?'=>'w',

        'Ý'=>'Y', 'Ÿ'=>'Y', '?'=>'Y',
        'ý'=>'y', 'ÿ'=>'y', '?'=>'y',

        'Ž'=>'Z', '?'=>'Z', '?'=>'Z',
        'ž'=>'z', '?'=>'z', '?'=>'z',

        '“'=>'"', '”'=>'"', '‘'=>"'", '’'=>"'", '•'=>'-', '…'=>'...', '—'=>'-', '–'=>'-', '¿'=>'?', '¡'=>'!', '°'=>' degrees ',
        '¼'=>' 1/4 ', '½'=>' 1/2 ', '¾'=>' 3/4 ', '?'=>' 1/3 ', '?'=>' 2/3 ', '?'=>' 1/8 ', '?'=>' 3/8 ', '?'=>' 5/8 ', '?'=>' 7/8 ',
        '÷'=>' divided by ', '×'=>' times ', '±'=>' plus-minus ', '?'=>' square root ', '?'=>' infinity ',
        '?'=>' almost equal to ', '?'=>' not equal to ', '?'=>' identical to ', '?'=>' less than or equal to ', '?'=>' greater than or equal to ',
        '?'=>' left ', '?'=>' right ', '?'=>' up ', '?'=>' down ', '?'=>' left and right ', '?'=>' up and down ',
        '?'=>' care of ', '?' => ' estimated ',
        '?'=>' ohm ',
        '?'=>' female ', '?'=>' male ',
        '©'=>' Copyright ', '®'=>' Registered ', '™' =>' Trademark ',
    );

    $string = strtr($string, $table);
    // Currency symbols: £¤¥€  - we dont bother with them for now
    $string = preg_replace("/[^\x9\xA\xD\x20-\x7F]/u", "", $string);

    return $string;
}
Run Code Online (Sandbox Code Playgroud)


Tom*_*zyk 2

使用 Normalizer PHP 扩展。

\n\n

http://www.php.net/manual/en/class.normalizer.php

\n\n
<?php\n$string = \'H\xc3\xb6hle\';\necho Normalizer::normalize($string);\n?>\n
Run Code Online (Sandbox Code Playgroud)\n

  • 好的。我如何在实践中使用它(Jak tego uzywac:P)? (3认同)
  • 我不知道为什么这个答案被接受,这根本不是规范化器所做的。 (3认同)
  • 哦,乡下人,多好啊。;]我已经更新了我的答案,如果您还有其他问题,请询问。(Polak,jak miło。;] W zaktualizowanej wersji masz odpowiedź wtwoim przypadku。) (2认同)
  • 没问题,很高兴认识你。;] (2认同)
  • 友情提醒,您可能已经“赞成”我的答案,而不是想要反对。我知道 Normalizer 的作用,那只是很好的副作用。如果您知道更好的*自动*答案,请编辑我的答案或与我联系 - 我很乐意学习新的解决方案。如果您不知道对OP有用的更好答案(除了下面的手动替换),请不要为了这样做而批评。 (2认同)