I have a PHP script that uses CURL to fetch the title and description of a user-entered URL and displays them on the page (which includes a utf-8 charset meta tag), and I'm having problems with characters not displaying correctly.
I read in this answer that the PHP CURL function encodes strings to utf-8 and that I need to decode strings with utf8_decode. But I'm finding that using utf8_decode is a hit or miss proposition -- sometimes it helps, sometimes, it creates unknown characters where there were none in the string before it was decoded.
I've included some examples below.
What's the proper way to handle encoding in this case?
Examples:
Here's the content fetched from a NY Times article with an emdash in the description. In this case, the decoded version displays the character properly:
Here's content from another NY Times article with an emdash in the description, and here, decoding made the character display improperly:
I'm finding that decoding causes problems with foreign language sites like this one in Spanish:
I know I can detect the language of the URL and decode or not based on that, but I'm finding plenty of English language sites where encoding causes problems, like this one: