Apr
28
2009

PHP parse text and change urls into links

Regex can be used to convert all urls in a plain text into links i.e. with anchor tags.
Code :

function parse_links($str)
{
    $str = str_replace('www.', 'http://www.', $str);
    $str = preg_replace('|http://([a-zA-Z0-9-\./]+)|', '<a href="http://$1">$1</a>', $str);
    $str = preg_replace('/(([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})/', '<a href="mailto:$1">$1</a>', $str);
    return $str;
}
?>

Popularity: 1% [?]

About the Author: Binary Tides

Leave a comment