PHP: Replacing special characters in a String
July 1, 2009
No comments
I recently needed to replace all Degree ( ° ) symbols with a dot ( . ) symbol in a String.
So I tried this: $result = str_replace("°" , "." , $mystr) ;
It did not work, because the String ($mystr) was encoded in UTF8, since it was read in from a file.
PHP could not properly compare the characters.
I fixed it by changing the default encoding of my string:
$mystr = utf8_encode ( $mystr) ;
After that the str_replace function returned the expected result.
It seems to be a good idea to always declare the encoding when loading local or remote files in PHP.
Comments
Leave a comment Trackback