帮助替换字符

时间:2011-08-10 00:39:39

标签: php

希望有人可以在这里提供帮助;

我正在尝试编写一个替换特殊字符并返回正确字符的函数。

这是我到目前为止所做的:

function convertlatin($output){ 

    $latinchar = array("€", "‚","Æ'","„","…","‡","ˆ","‰","Å","‹","Å'",'Ž','‘','’','“','â€','•','â€"','â€"','Ëœ','â"¢','Å¡','›','Å"',"ž",'Ÿ','¡','¢','£','¤','Â¥','¦','§','¨','©','ª','«','¬','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼',"½",'¾','¿','À','Ã','Â','Ã','Ã"','Ã…','Æ','Ç','È','É','Ê','Ë','ÃŒ    ','Ã','ÃŽ','ß','Ã',"Ã'","Ã'",'Ã"','Ã"','Õ','Ö','×','Ø','Ù','Ú','Û','Ãœ','Ã','Þ','ß','Ã','á','â','ã','ä','Ã¥','æ','ç','è','é','ê','ë','ì','Ã','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý',"þ","ÿ");
    $correctchar = array("€", "‚","ƒ",'"','…','‡','ˆ','‰',"Š",'‹','Œ','Ž',"'","'",'"','"','•','–','—','˜','™','š','›','œ','ž','Ÿ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ',"ÿ");

    $returnval = str_replace($latinchar, $correctchar, $output);

    echo($returnval);
    return $returnval;
}

我遇到的问题是我认为它有效,但它有随机结果,例如,如果它只找到一个字符匹配,它会替换该数组中的另一个字符。我想要做的是在提供的字符串中找到并替换拉丁字符的完全匹配,例如“testingÿ”和“testingÿ” - 在它用testá¿替换ÿ

在我希望匹配和替换这两个参数时,它似乎在某些情况下替换了一个字符。

我也尝试过strcmp并没有太大的成功。

有什么想法吗?

1 个答案:

答案 0 :(得分:6)

看起来你的问题没有错误的字符,这是一个错误的编码。也许你最好尝试改变$ output的编码。 utf8_encode不会帮助你,“错误的”字符看起来像是一些错误的转换Windows-1252输入。

尝试:

echo mb_convert_encoding('testingÿ','CP1252','UTF-8');