将重音字符转换为其简单的ascii等效项

时间:2021-02-01 00:28:54

I have to convert french characters into english on my php. I've used the following code:

我必须在我的PHP上将法语字符转换成英语。我使用了以下代码:

iconv("utf-8", "ascii//TRANSLIT", $string);

But the result for ËËË was "E"E"E.

但ËËË的结果是“E”E“E。

I don't need that double quote and other extra characters - I want to show an output like EEE. Is there any other method to convert french to english? Can you help me to do this?

我不需要双引号和其他额外字符 - 我想显示像EEE这样的输出。还有其他方法可以将法语翻译成英语吗?你能帮帮我吗?

4 个解决方案

#1


37  

The PHP Manual iconv Intro has a warning:

PHP手册iconv简介有一个警告:

Note that the iconv function on some systems may not work as you expect. In such case, it'd be a good idea to install the GNU libiconv library. It will most likely end up with more consistent results.

请注意,某些系统上的iconv功能可能无法正常工作。在这种情况下,安装GNU libiconv库是个好主意。它最有可能最终得到更一致的结果。

But if accented characters are the only issue then you could use a dirty strtr (partially from strtr comments):

但如果重音字符是唯一的问题,那么你可以使用脏strtr(部分来自strtr注释):

$string = 'Ë À Ì Â Í Ã Î Ä Ï Ç Ò È Ó É Ô Ê Õ Ö ê Ù ë Ú î Û ï Ü ô Ý õ â û ã ÿ ç';

$normalizeChars = array(
    'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
    'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
    'Ï'=>'I', 'Ñ'=>'N', 'Ń'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
    'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
    'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
    'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ń'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
    'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f',
    'ă'=>'a', 'î'=>'i', 'â'=>'a', 'ș'=>'s', 'ț'=>'t', 'Ă'=>'A', 'Î'=>'I', 'Â'=>'A', 'Ș'=>'S', 'Ț'=>'T',
);

//Output: E A I A I A I A I C O E O E O E O O e U e U i U i U o Y o a u a y c
echo strtr($string, $normalizeChars);

#2


16  

This worked for me for French characters.

这对我来说对法语字符很有用。

$str = utf8_encode($str);
$str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);

#3


6  

An alternative:

替代:

function replaceAccents($str) {

  $search = explode(",","ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,ø,Ø,Å,Á,À,Â,Ä,È,É,Ê,Ë,Í,Î,Ï,Ì,Ò,Ó,Ô,Ö,Ú,Ù,Û,Ü,Ÿ,Ç,Æ,Œ");

  $replace = explode(",","c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,o,O,A,A,A,A,A,E,E,E,E,I,I,I,I,O,O,O,O,U,U,U,U,Y,C,AE,OE");

  return str_replace($search, $replace, $str);
}


$str = "À é ü ä ç";
$str = replaceAccents($str);
echo "$str \n"; 
//output "A e u a c" 

#4


2  

Here is the wordpress way:

这是wordpress方式:

http://codex.wordpress.org/Function_Reference/remove_accents

http://codex.wordpress.org/Function_Reference/remove_accents

You can copy the remove_accents() function and implement to your system.

您可以将remove_accents()函数复制并实现到您的系统。

https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/formatting.php#L682

https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/formatting.php#L682

#1


37  

The PHP Manual iconv Intro has a warning:

PHP手册iconv简介有一个警告:

Note that the iconv function on some systems may not work as you expect. In such case, it'd be a good idea to install the GNU libiconv library. It will most likely end up with more consistent results.

请注意,某些系统上的iconv功能可能无法正常工作。在这种情况下,安装GNU libiconv库是个好主意。它最有可能最终得到更一致的结果。

But if accented characters are the only issue then you could use a dirty strtr (partially from strtr comments):

但如果重音字符是唯一的问题,那么你可以使用脏strtr(部分来自strtr注释):

$string = 'Ë À Ì Â Í Ã Î Ä Ï Ç Ò È Ó É Ô Ê Õ Ö ê Ù ë Ú î Û ï Ü ô Ý õ â û ã ÿ ç';

$normalizeChars = array(
    'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
    'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
    'Ï'=>'I', 'Ñ'=>'N', 'Ń'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
    'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
    'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
    'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ń'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
    'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f',
    'ă'=>'a', 'î'=>'i', 'â'=>'a', 'ș'=>'s', 'ț'=>'t', 'Ă'=>'A', 'Î'=>'I', 'Â'=>'A', 'Ș'=>'S', 'Ț'=>'T',
);

//Output: E A I A I A I A I C O E O E O E O O e U e U i U i U o Y o a u a y c
echo strtr($string, $normalizeChars);

#2


16  

This worked for me for French characters.

这对我来说对法语字符很有用。

$str = utf8_encode($str);
$str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);

#3


6  

An alternative:

替代:

function replaceAccents($str) {

  $search = explode(",","ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,ø,Ø,Å,Á,À,Â,Ä,È,É,Ê,Ë,Í,Î,Ï,Ì,Ò,Ó,Ô,Ö,Ú,Ù,Û,Ü,Ÿ,Ç,Æ,Œ");

  $replace = explode(",","c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,o,O,A,A,A,A,A,E,E,E,E,I,I,I,I,O,O,O,O,U,U,U,U,Y,C,AE,OE");

  return str_replace($search, $replace, $str);
}


$str = "À é ü ä ç";
$str = replaceAccents($str);
echo "$str \n"; 
//output "A e u a c" 

#4


2  

Here is the wordpress way:

这是wordpress方式:

http://codex.wordpress.org/Function_Reference/remove_accents

http://codex.wordpress.org/Function_Reference/remove_accents

You can copy the remove_accents() function and implement to your system.

您可以将remove_accents()函数复制并实现到您的系统。

https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/formatting.php#L682

https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/formatting.php#L682