Also inspired by Akarun's answer, the following function will turn to links only the text that is not already a link. The added functionality is checking that a link with the captured text link doesn't already exists in the target string:
function make_links_from_http($content) {
// Links out of text links
preg_match_all('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', $content, $matches);
foreach ($matches[0] as $key=>$link) {
if (!preg_match('!<a(.*)'.$link.'(.*)/a>!i', $content))
{
$content = str_replace($link, '<a href="'.$link.'" target="_blank">'.$link.'</a>', $content);
}
}
return $content;
}
By testing, I noticed that the above function fails on line #5. A "messier" function that does the job is the following:
function make_links_from_http($content)
{
// The link list
$links = array();
// Links out of text links
preg_match_all('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', $content, $matches);
foreach ($matches[0] as $key=>$link)
{
$links[$link] = $link;
}
// Get existing
preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $content, $matches);
foreach ($matches[2] as $key=>$value)
{
if (isset($links[$value]))
{
unset($links[$value]);
}
}
// Replace in content
foreach ($links as $key=>$link)
{
$content = str_replace($link, '<a href="'.$link.'" target="_blank">'.$link.'</a>', $content);
}
return $content;
}
For the new code, I have used the tutorial at:
http://www.the-art-of-web.com/php/parse-links/