Skip to content

Instantly share code, notes, and snippets.

@coreyweb
Created March 25, 2012 14:00
Show Gist options
  • Save coreyweb/2194781 to your computer and use it in GitHub Desktop.
Save coreyweb/2194781 to your computer and use it in GitHub Desktop.
Twitter: rewrite URLs and replies as links
<?php
// Convert URL's with protocol prefix
$text = ereg_replace("[a-zA-Z]+://([-]*[.]?[a-zA-Z0-9_/-?&%])*", "<a href=\"\\0\">\\0</a>", $text);
//Convert URL with just www.
$text = ereg_replace("(^| |\n)(www([-]*[.]?[a-zA-Z0-9_/-?&%])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text);
//Convert @ replies
$text = ereg_replace("(^| |\n)(\@([-]*[.]?[a-zA-Z0-9_/-?&%])*)", "\\1<a href=\"http://www.twitter.com/\\2\">\\2</a>", $text);
$text = str_replace("/@", "/", $text);
return $text;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment