Php str_replace not working with special chars

前端 未结 3 1706
终归单人心
终归单人心 2021-01-12 23:29

why isn\'t this working as expected:

 echo str_replace(\"é\",\"é\",\"Fédération Camerounaise de Football\");

result:

\"F         


        
3条回答
  •  轮回少年
    2021-01-13 00:20

    //edit after comment

    Fédération Camerounaise de Football is an UTF-8 encoded string so i don't know what input is not utf-8 encoded in your document but you have two options.

    1. your input that are passed to str_replace is utf-8 but the characters that you have used in the functions to replace are ANSII or something else => not work - this means your document is not utf-8 - this is why uft8_decode works str_replace(ANSII, ANSII, CONVERT_TO_ANSII(UTF-8))

    2. your input is not utf-8 and your document is - so this would work str_replace(UTF-8, UTF-8, CONVERT_TO_UTF-8(ANSII))


    str_replace works great with multibyte characters - your problem is not the function its is because you try to replace different encoding types. instead of using a alternative function - i suggest you to fix the input that are passed to str_replace to utf-8 and make sure that your document is utf-8 encoded too.

    if your source only support non utf-8 encoding use utf8_encode to convert your input to utf-8

    http://php.net/manual/de/function.utf8-encode.php

提交回复
热议问题