views:

168

answers:

1

On this page...

http://www.axisofeco.com/matters/2010/03/11/the-word-youre-looking-for-is-sublime/

... I have a 'Tweet this' link which pastes in the article title and a link into your Twitter update box (if you're logged in to Twitter).

Problem is, when articles such as the one above have special characters in them (fancy single / double quotes mostly), Twitter can't seem to handle them. This is what Twitter tries (and fails horribly) to paste into the Twitter update box:

8217;re+looking+for+is+“sublime”+http://is.gd/acIW2

(if quotes are actually looking ok in the above line, trust me, they stay encoded when Twitter tries to deal with them!)

The relevant PHP for formatting the Twitter update is this:

$url = str_replace(' ', '+', $url); 
echo htmlentities($url, ENT_COMPAT, 'UTF-8');

I've tried using different functions, for example, using urlencode(), but nothing seems to encode the string ($url) in a 'Twitter friendly' manner.

+1  A: 

A simple urlencode() worked for me:

Tweet This

//make sure you only urlencode the get var
echo urlencode("The word you’re looking for is “sublime”");
//ran the above on php and used the result in the link

However, I'd just replace 'pretty' quotes with normal quotes before forming the URL. Depending on the browser/location, the resulting tweet may not look correct to other users.

Tim Lytle
This is strange. When I run that exact code above I get -The+word+you%92re+looking+for+is+%93sublime%94 - as the output. I get this on the axisofeco.com server and my local server.
Jack
Perhaps the problem is that the server/PHP doesn't understand the unicode character to begin with. While it works on my server, I wouldn't use that in production.
Tim Lytle