/ Published in: PHP

This small function receive a text as input and returns an html text with links if the source text contains urls (http://www… but also ftp://… and every other protocol), emails, twitter’s usernames (with @ at the beginning) and also twitter tags (with # at the beginning).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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); }
URL: http://www.barattalo.it/2010/03/10/php-parse-url-mailto-and-also-twitters-usernames-and-arguments/
Comments
