I’ve been using FeedWordPress and a Yahoo Pipe that filters out replies to post my Tweets on this blog. However, the only problem has been that Twitter’s RSS feeds don’t turn URLs into hyperlinks, which may be fine in an RSS feed, but kind of sucks in a blog post. But at least on the blog posts, I now have a fix by using this code to print out each Tweet in my template:

First try:
<?php
echo wptexturize(preg_replace('/(http:\/\/\S+)/',
'<a href="$1">$1</a>',$post->post_title));
?>

Second (better) try:
<?php
echo wptexturize(preg_replace
('/(http:\/\/((.\S)|([A-Za-z0-9\/\?\_\-\=\#\&\~]))+)/',
'<a href="$1">$1</a>',$post->post_title));
?>

Ok, third times the charm:
<?php
echo wptexturize(preg_replace
('/(http:\/\/((.[a-zA-Z0-9])|([A-Za-z0-9\/\?\_\-\=\#\&\~]))+)/',
'<a href="$1">$1</a>',$post->post_title));
?>

Seriously, last time…I think. Found here, but modified so as to leave any url already inside a hyperlink alone (Read “Update 4″ below for the explanation) and also to better handle non-URL characters immediately after the URL:

< ?php 
    echo wptexturize(preg_replace
    ('@(([^">])(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?[^.\ ,:)]))@', '$2<a href="$3">$3</a>', $post->post_title));
?>

This is actually a pretty weak all-purpose URL pattern, but it works just fine for the kind of links I’d have in a Twitter post.

Update: Ok, a flaw in the plan. “\S” matches any non-white space character, which obviously includes a “.” so if you put a period after a URL at the end of a sentence, it counts that as part of the URL. Don’t have time to fix it now, but I’ll try later. This is the point at which regular expressions make my brain hurt, so we’ll see.

Update 2: Ok, I think I fixed that problem. The old code is strikethrough’d and the new code is below it. This probably will mess up with some URLs too, but since most crazy URLs on Twitter get passed through tinyurl anyway, hopefully this will work most of the time.

Update 3: For reasons I can’t explain, '.\S', which is supposed to match any dot followed by a non-white space character (right?), was sometimes matching spaces for reasons I can’t understand. For example, it matched all of 'http://url.com - a good site', turning it into one long, run-on URL. Similarly, it matched all of this: 'http://url.com/foo. Go read.'. However, it correctly handled 'http://url.com/foot. Go read.' and 'http://url.com/fo. Go read.'. So there’s clearly something about either regular expressions or preg_replace in particular I’m missing.

So i just tightened that part up to be .[a-zA-Z0-9] so that it only matches a dot followed by an alphanumeric character, which should be the only kind of character following a dot in a url anyway…well, at least until I realize some soon-to-be obvious exception and have to change this again.

Update 4: So I stopped using the Yahoo Pipe and replaced it with a php script that does both the @-stripping & “username: ” stripping, but also the auto-hyperlinking I used to do in the wordpress theme (never was the best idea anyway). So this is a much better system, but it left me with a bit of a challenge: how to preserve the presentation of the old posts (without hyperlinks) without interfering with the new ones. And here’s how:

        < ?php
            $tweetpost=$post->post_content;
            $tweetpost=str_replace("smajda: ","",$tweetpost);
            $tweetpost=preg_replace('@(([^">])(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?[^.\ ,:)]))@','$2<a href="3">$3</a>', $tweetpost);
        ?>