php - Replacing non UTF8 characters
In php, I need to replace all non-UTF8 characters in a string. However, not by some equivalent (like theiconv
function with//TRANSLIT
) but by some chosen character (like"_"
or"*"
for example).
Typically I want the user to be able to see the position were the invalid characters were found.
I didn't find any functions that do this, so I was going to use:
- use
iconv
with//IGNORE
- do a diff on the two strings and insert the wanted character where the non-UTF8 ones where
Do you see a better way to do that, is there some functions in php that can be combined to have this behavior ?
Thanks for you help.
Answer
Solution:
Here are 2 functions to help you achieve something close to what you want :
note that you can change the replacement (which currently is '?' with anything else by changing the string located at
preg_replace('blablabla', **'?'**, $some_string)
the original article : http://magp.ie/2011/01/06/remove-non-utf8-characters-from-string-with-php/