/ Published in: PHP
URL: http://www.barattalo.it/2010/03/10/php-parse-url-mailto-and-also-twitters-usernames-and-arguments/
Text parsing for Twitter text attribute - segregating and styling output for link URLs, link mailtos, Twitter users (@ replies), Twitter arguments, and truncation for long URLs.
Expand |
Embed | Plain Text
function parse_twitter($t) { // link URLs "([[:alnum:]#?\/&=])/i", "<a href=\"\\1\\3\\4\" target=\"_blank\">". "\\1\\3\\4</a>", $t); // link mailtos "([[:alnum:]-]))/i", "<a href=\"mailto:\\1\">\\1</a>", $t); //link twitter users $t = preg_replace( "/ +@([a-z0-9_]*) ?/i", " <a href=\"http://twitter.com/\\1\" target=\"_blank\">@\\1</a> ", $t); //link twitter arguments $t = preg_replace( "/ +#([a-z0-9_]*) ?/i", " <a href=\"http://twitter.com/search?q=%23\\1\" target=\"_blank\">#\\1</a> ", $t); // truncates long urls that can cause display problems (optional) "{30,40})([^[:space:]]*)([^[:space:]]{10,20})([[:alnum:]#?\/&=])". "</", ">\\3...\\5\\6<", $t); }
You need to login to post a comment.
