Created
September 5, 2012 13:30
-
-
Save fabrizim/3636555 to your computer and use it in GitHub Desktop.
PHP Autolink Text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fabs_linkify($str){ | |
$re = "/(([a-z0-9\$\-\_\.\+\!\*'\(\)\,]+)\@)?((https?\:\/\/)?(www\.)?([a-z0-9\$\-\_\.\+\!\*'\(\)\,\/]+)\.(com|org|net|gov|us|tv)(\/[a-z0-9\$\-\_\.\+\!\*'\(\)\,\/]+)?)/i"; | |
return preg_replace_callback($re, 'fabs_linkify_callback', $str); | |
} | |
function fabs_linkify_callback($matches){ | |
if( $matches[1] ) { | |
return '<a href="mailto:'.$matches[0].'">'.$matches[0].'</a>'; | |
} | |
else{ | |
$url = $matches[0]; | |
if( strpos($url, 'http') !== 0 ){ | |
$url = 'http://'.$url; | |
} | |
return '<a href="'.$url.'" target="_blank">'.$url.'</a>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment