-2

I am trying to replace a string which does not have any foreign characters in it with the string that has foreign keys in it.

If I do as I show in the following code:

$str= str_replace($original, utf8_encode($replace), $str);

or

$str= utf8_encode(str_replace($original, $replace, $str));

I get Fédération for Fédération

Any idea how to make str_replace or preg_replace to work with this situation?

1 Answer 1

0

This problem is happening because you are utf8_encoding a string that is already in UTF-8 format.

Try to use this one:

$str = mb_convert_encoding($str, 'UTF-8', mb_detect_encoding($str));

This will find the current string format and encode it correctly.

PS: This way will prevent you to get your string buggy, then you can use the str_replace correctly.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.