我想从以下字符中删除重音,我已经考虑过以下解决方案,这可能有效,但感觉不对。有没有人有更聪明的方法呢?
$document = str_replace('ç','c',$document); // get rid of cedille
$document = str_replace('Ç','c',$document);
$document = str_replace('é','e',$document); // get rid of accents on E
$document = str_replace('è','e',$document);
$document = str_replace('ë','e',$document);
$document = str_replace('ê','e',$document);
$document = str_replace('È','e',$document);
$document = str_replace('É','e',$document);
$document = str_replace('Ê','e',$document);
$document = str_replace('Ë','e',$document);
$document = str_replace('À','a',$document); // get rid of accents on A
$document = str_replace('Á','a',$document);
$document = str_replace('Â','a',$document);
$document = str_replace('Ã','a',$document);
$document = str_replace('Ä','a',$document);
$document = str_replace('Å','a',$document);
$document = str_replace('à','a',$document);
$document = str_replace('á','a',$document);
$document = str_replace('â','a',$document);
$document = str_replace('ã','a',$document);
$document = str_replace('ä','a',$document);
$document = str_replace('å','a',$document);
$document = str_replace('Æ','ae',$document); // get rid of Æ and Œ
$document = str_replace('æ','ae',$document);
$document = str_replace('Œ','oe',$document);
$document = str_replace('œ','oe',$document);
$document = str_replace('ì','i',$document); //get rid of accents on I
$document = str_replace('í','i',$document);
$document = str_replace('î','i',$document);
$document = str_replace('ï','i',$document);
$document = str_replace('Ì','i',$document);
$document = str_replace('Í','i',$document);
$document = str_replace('Î','i',$document);
$document = str_replace('Ï','i',$document);
$document = str_replace('ñ','n',$document); // get rid of accent on N
$document = str_replace('Ñ','n',$document);
$document = str_replace('ò','o',$document); // get rid of accents on O
$document = str_replace('ó','o',$document);
$document = str_replace('ô','o',$document);
$document = str_replace('õ','o',$document);
$document = str_replace('ö','o',$document);
$document = str_replace('Ò','o',$document);
$document = str_replace('Ó','o',$document);
$document = str_replace('Ô','o',$document);
$document = str_replace('Õ','o',$document);
$document = str_replace('Ö','o',$document);
$document = str_replace('ù','u',$document); // get rid of accents on U
$document = str_replace('ú','u',$document);
$document = str_replace('û','u',$document);
$document = str_replace('ü','u',$document);
$document = str_replace('Ù','u',$document);
$document = str_replace('Ú','u',$document);
$document = str_replace('Û','u',$document);
$document = str_replace('Ü','u',$document);
$document = str_replace('ý','y',$document); // get rid of accents on Y
$document = str_replace('ÿ','y',$document);
$document = str_replace('Ý','y',$document);
$document = str_replace('Ÿ','y',$document);
$document = str_replace('ž','z',$document); // get rid of accents on Z
$document = str_replace('Ž','z',$document);
编辑以回答评论:for / foreach循环只是移动问题,基本上我想知道是否有任何方法可以避免使用str_replace()50(〜)次。
答案 0 :(得分:3)
您可以使用iconv + ASCII//TRANSLIT
。
$text = 'ç œ æ Ê è';
$text = strtolower(iconv("UTF-8", "ASCII//TRANSLIT", $text));
echo $text; // print "c oe ae e e"
将UTF-8
替换为您正在使用的编码。
由于ASCII是许多字符集的子集(如utf-8),因此无需将其转换回原始编码。