Parse URL’s in text, create links
I’m absolutely in love with the status update stream I’ve put together for Fwd:Vault (follow link for example). However in the process, I’ve discovered a huge drawback to the Twitter messaging system: it does not store links. The Twitter site itself will identify URL’s in messages and convert them into clickable links for you automatically. But the magic ends at Twitter’s borders; anyone who wants to do the same on their site is on their own.
So I consulted the almighty Google. I found plenty of raw regex, javascript, and Twitter-focused discussions on the matter, but I found the offered solutions and tips lacking. I wanted to do this up right, transparently via PHP in the background. No JS required.
Finally, I found a small PHP script that accomplished what I needed. Here’s a renamed version—all code intact—that will find and convert any well-formed URL into a clickable <a> tag link.
Update: My buddy Tonk has updated the code to link up @replies and #hashtags as well. He also switched from POSIX to Perl regular expressions syntax, mostly cause he’s a regex dork.
function linkify( $text ) { $text = preg_replace( '/(?!<\S)(\w+:\/\/[^<>\s]+\w)(?!\S)/i', '<a href="$1" target="_blank">$1</a>', $text ); $text = preg_replace( '/(?!<\S)#(\w+\w)(?!\S)/i', '<a href="http://twitter.com/search?q=#$1" target="_blank">#$1</a>', $text ); $text = preg_replace( '/(?!<\S)@(\w+\w)(?!\S)/i', '@<a href="http://twitter.com/$1" target="_blank">$1</a>', $text ); return $text; }
Copy that into your code, then run your text containing unlinked URL’s through it. Let’s apply it to the Twitter feed example as we left it in Step 2:
<li><?php echo linkify($status->text) . '<br />' . $time_display; ?></li>
You can find this code at work on Fwd:Vault.
Build a slick Twitter feed on your site
- Display Twitter updates on your website
- Calculate dates and times in different timezones (translate Twitter timestamps)
- Parse URL’s in text, create links




Data security without software or hardware
Had trouble with your code, lost the link text and it Linked ever Tweet below with a false link. I tried this out and it worked Great, not sure the difference…
(It links the Hash Tags and the @danfoxx too.)
function linkify($status_text)
{
// linkify URLs
$status_text = preg_replace(
‘/(https?:\/\/\S )/’,
‘\1‘,
$status_text
);
// linkify twitter users
$status_text = preg_replace(
‘/(^|\s)@(\w )/’,
‘\1@\2‘,
$status_text
);
// linkify tags
$status_text = preg_replace(
‘/(^|\s)#(\w )/’,
‘\1#\2‘,
$status_text
);
return $status_text;
}
I too had some issues with the original code. Dan, I tried your code and still could not get it to work. Are you still using the same output method?
I tried this. I changed the name of variable from $t to $tweettext and then I updated the ereg_replace statement.
function linkify($tweettext) {
$tweettext = ereg_replace(“[[:alpha:]] ://[^[:space:]] [[:alnum:]/]”,”\“, $tweettext);
return $tweettext;
}
It worked for me. Although I am real interested to see how Dan’s solution works.
Here is my Entire Code… Works great, check out the Links Page of my site…
http://thefoxxfamily.com/
CODE:
<?php
function twitter_status($twitter_id, $hyperlinks = true) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/15456263.xml");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($c, CURLOPT_TIMEOUT, 5);
$response = curl_exec($c);
$responseInfo = curl_getinfo($c);
curl_close($c);
if (intval($responseInfo['http_code']) == 200) {
if (class_exists('SimpleXMLElement')) {
$xml = new SimpleXMLElement($response);
return $xml;
} else {
return $response;
}
} else {
return false;
}
}
function linkify_twitter_status($status_text)
{
// linkify URLs
$status_text = preg_replace(
'/(https?:\/\/\S )/',
'\1‘,
$status_text
);
// linkify twitter users
$status_text = preg_replace(
‘/(^|\s)@(\w )/’,
‘\1@\2‘,
$status_text
);
// linkify tags
$status_text = preg_replace(
‘/(^|\s)#(\w )/’,
‘\1#\2‘,
$status_text
);
return $status_text;
}
if ( $twitter_xml = twitter_status(‘12345678′) ) {
date_default_timezone_set(‘GMT’);
$tz = new DateTimeZone(‘America/New_York’);
$i = 0;
foreach ($twitter_xml->status as $key => $status) {
$datetime = new DateTime($status->created_at);
$datetime->setTimezone($tz);
$time_display = $datetime->format(‘D, M jS g:ia T’);
?>
text) . ” . $time_display; ?>
read more updates…
Stupid Wordpress editor mangled the code. It’s fixed, try the above.
I’ve updated the linkify() function to include support for turning #hashtags into links as well (excuse my having converted from ereg_* to preg_* – just a comfort-zone thing).
function linkify( $text ) { $text = preg_replace( '/(?!< \S)(\w+:\/\/[^<>\s]+\w)(?!\S)/i', '$1', $text ); $text = preg_replace( '/(?!< \S)#(\S+\w)(?!\S)/i', '#$1', $text ); return $text; }Here’s an[other] updated linkify() function that turns @account tokens into Twitter account links as well. Let’s hope WordPress doesn’t mangle this one too badly…
function linkify( $text ) { $text = preg_replace( '/(?!<\S)(\w+:\/\/[^<>\s]+\w)(?!\S)/i', '<a href="$1" target="_blank">$1</a>', $text ); $text = preg_replace( '/(?!<\S)#(\w+\w)(?!\S)/i', '<a href="http://twitter.com/search?q=#$1" target="_blank">#$1</a>', $text ); $text = preg_replace( '/(?!<\S)@(\w+\w)(?!\S)/i', '@<a href="http://twitter.com/$1" target="_blank">$1</a>', $text ); return $text; }