Search code examples
phpiconvutf8-decode

Can php convert strings with all charset encodes to utf8


Can php convert strings with all charset encodes to utf8?

Solutions that don't works:

  1. utf8_encode($string) - but its only Encodes an ISO-8859-1 string to UTF-8?
  2. iconv($incharset, $outcharset,$text) - but how can be find string current encodding? (only can be if string part of html dom document, not just string)

thanks


Solution

  • It is possible to convert a string from any encoding supported by iconv() into UTF-8 in PHP.

    but how can be find string current encodding?

    You should never need to "find" the current encoding: Your script should always know what it is. Any resource you query, if properly encoded, will give you its encoding in the content-type header or through other means.

    As Artefacto says, there is the possibility of using mb_detect_encoding() but this is not a reliable method. The data flow of the program should always have it defined what encoding a string is in (and preferably work with UTF-8 internally) - that's the way to go.