Advertisement
iagdotme

twitterize

Mar 19th, 2015
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.58 KB | None | 0 0
  1. // The function that links the hashtags, urls and usernames into links...
  2. function twitterize($raw_text) {
  3.     $output = $raw_text;
  4.  
  5.     // parse urls;
  6.     $output = preg_replace(
  7.     '@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@', '<a href="$1" target="_blank">$1</a>', $output);
  8.  
  9.     // parse usernames
  10.     $output = preg_replace(
  11.     '/@(\w+)/', '<a href="http://twitter.com/$1" target="_blank">@$1</a>',$output);
  12.  
  13.     // parse hashtags
  14.     $output = preg_replace(
  15.     '/\s+#(\w+)/', ' <a href="https://twitter.com/search?q=%23$1" target="_blank">#$1</a>', $output);
  16.  
  17.     return $output;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement