mro
mro

Reputation: 141

Help with replacing characters

Hopefully someone can help out here;

I am trying to write a function which replaces special characters and returns the correct one.

This is what I have so far:

function convertlatin($output){ 

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

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

    echo($returnval);
    return $returnval;
}

The problem I have is I thought it was working but it has random results, such as if it finds a match on just one of the characters it replaces a different one in that array. What I would like to do is find and replace an exact match of latin char within a supplied string eg "testingÿ" with "testingÿ" - at the mo it replaces ÿ with testingá¿

It just seems to replace one character in some occasions, when I would like it to match and replace both parameters.

I also tried strcmp with not much success.

Any ideas ?

Upvotes: 0

Views: 6022

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117314

Looks like your problem is not wrong chars, it's more a wrong encoding. Maybe you better try to change the encoding of $output. utf8_encode will not help you, the "wrong" chars look like some wrong converted Windows-1252-input.

Try:

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

Upvotes: 6

Related Questions