Possible Duplicate:
php regex to match outside of html tags
I found a nice function here: https://stackoverflow.com/a/1945957
It converts text URLs to proper links, but it also matches URLs in tags such as <img>
. Can the function be modified to only match URLs that are not in quotes (single or double)?
Thanks
/**
* Replace links in text with html links
*
* @param string $text
* @return string
*/
function auto_link_text($text)
{
$pattern = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
$callback = create_function('$matches', '
$url = array_shift($matches);
$url_parts = parse_url($url);
$text = parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH);
$text = preg_replace("/^www./", "", $text);
$last = -(strlen(strrchr($text, "/"))) + 1;
if ($last < 0) {
$text = substr($text, 0, $last) . "…";
}
return sprintf(\'<a rel="nofollow" href="%s">%s</a>\', $url, $text);
');
return preg_replace_callback($pattern, $callback, $text);
}
Input:
<img src = "http://www.google.com/logo.png" /> http://www.google.com
Expected output:
<img src = "http://www.google.com/logo.png" /> <a rel="nofollow" href="http://www.google.com">http://www.google.com</a>
Solved:
#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))(?=[^>]*(<|$))#