Simon Bingham
Simon Bingham

Reputation: 1310

Twitter API character encoding issue in PHP

I'm trying to integrate Twitter API V1.1 with a PHP website. On Twitter my tweet appears as:

SWLaw : Farm & Country is out! http://paper.li/SWLAWCOUK/1346753760 … ▸ Top stories today via @CountrySportsSW @jonesFW @NFUBFGmag

However, on my site the tweet appears as:

RT @SWLawCountry: SWLaw : Farm & Country is out! http://t.co/AU7WCxUxf5â–¸ Top stories today vi@CountrySportsSWS@jonesFWF@NFUBFGmagag

Note the additional characters and special characters.

This seems to be a character encoding issue because the Twitter API (I believe) returns JSON encoded as UTF-8 whereas my web page is encoded as iso-8859-1. I've attempted to fix the issue using PHP's iconv function as follows:

$new_text = iconv("UTF-8", "ISO-8859-1//IGNORE", $new_text);

This produces the following output:

RT @SWLawCountry: SWLaw : Farm & Country is out! http://t.co/AU7WCxUxf5 Top stories today vi@CountrySportsSWS@jonesFWF@NFUBFGmagag

Note that although the special characters have been removed, the additional characters remain and there are missing spaces between some words.

Does anyone know how I can overcome this problem?

Upvotes: 1

Views: 2345

Answers (1)

deceze
deceze

Reputation: 522032

The best thing to do would be to use UTF-8 for your site. This is pretty much the de-facto standard on the web these days and can encode virtually any and all characters in use on computers to date.

If it's difficult for you to switch over, you can represent characters that are not encodable in ISO-8859 by encoding them as HTML entities instead:

echo htmlentities($new_text, ENT_NOQUOTES, 'UTF-8');

This should preserve all characters, even in an ISO-8859 context.

Upvotes: 4

Related Questions