Skip to content

Instantly share code, notes, and snippets.

@fabrizim
Created September 5, 2012 13:30
Show Gist options
  • Save fabrizim/3636555 to your computer and use it in GitHub Desktop.
Save fabrizim/3636555 to your computer and use it in GitHub Desktop.
PHP Autolink Text
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