28
MAY
22:52

Umlauts in hash tags in Twitter Tools

I use the plugin Twitter Tools to import tweets into my blog which I put in my “Lifestream” to the right. It works really good, except that it seams to have a problem with Swedish characters like Å, Ä or Ö. I haven’t had the time or the will to try to fix it. But tonight … I did.

So here is the problem:


The tag should be #GöteborgsVarvet.

I started with The Google and found this thread on the WordPress support forum. But I didn’t like the look of it since it just adds to the reg exp character list. I could have just added my missing characters. But what if a Norwegian dude gets Internet and starts using The Twitter. That won’t do.

So I searched some more and came up with this solution. First I changed the reg exp to this:

'/(^|\s)#([\w\pL]{1,})(\W)/u'

It will search for any “word character” using unicode. Which should make it find å, ä, and ö. But also “the other ones”. From other languages. Then it breaks the tag when it finds any non-word character. For example a “-” or a “(” or something like that. This brings it closer to how Twitter does it:


What we see here is that Twitter gets all the strange tags, even with å or ö in them, but it breaks at the dash character.

However, I still had a problem with the reg exp. It couldn’t find tags in a row. If the tweet had three tags in a row it only matched the first and the last. I solved this, ugly, by first changing all white spaces to double white spaces. And after the hash tag match I change them back. So the full code now looks like this:

$tweet = preg_replace("/(\s)/", "  ", $tweet);
$tweet = preg_replace_callback(
	'/(^|\s)#([\w\pL]{1,})(\W)/u'
	, create_function(
		'$matches'
		, 'return aktt_hashtag_link($matches[2], \' #\', \'\');'
	)
	, $tweet
);
$tweet = preg_replace("/(\s\s)/", " ", $tweet);

The only problem left now is that if you have a tag with a dash, it will remove the dash. So instead of “#åsa-nisse” with a link on “åsa”, like Twitter does. It outputs “#åsa nisse” with a link on “åsa”. Like this:


All tags are found, but the dash is missing from “åsa-nisse”.

Now I just hope that the guys over at Crows Favorite sees my e-mail and fixes this in the official release. Hopefully with an even nicer solution.

By the way…

To fix this hack, from home, on a Saturday, I finally bought Coda in the Mac App Store. It’s a bit expensive. I’m using Eclipse at work, which is free and has more features. I really miss the autocomplete features. But for quick hacks on Saturdays, Coda is really nice. Eclipse is bloaty and slow. I hope I can find some good plugins for autocomplete or that Panic can add it. I would love to be able to Switch. Coda feels more like Xcode 4. I would love to be able to do all my work in Xcode 4. But since I code PHP and not Objective-C at work … Well. Let’s give Panic some money by buying Coda and hope for the best! /rant

Time to go and give my Emma some cuddle before she gets totally bored out of her mind. Peace.

Posted by David