Reputation: 1686
Im using the following regex to convert urls to href links. It works great, however ive found a bug with it when using style tags which have a background image.
/**
* Convert urls in a string to a html link
* @return string
*/
public static function ConvertUrlsToHtml($str)
{
$str = preg_replace( '@(?<![.*">])\b(?:(?:https?|ftp|file)://|[a-z]\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]@i', '<a href="\0">\0</a>', $str);
return $str;
}
If i use the following...
<div class="inner-left" style="background-image: url(http://www.somewebsite/background.jpg);"></div>
It converts the background image to a href too.
Does anyone know how i can tweak the regex to ignore the style tags?
Upvotes: 0
Views: 107
Reputation: 14681
You can start by removing HTML tags, because you don't want to replace URLs inside tags. It is true for style=
, it is also true for <img src=...
and <a href=...>
and so on.
function ConvertUrlsToHtml($str)
{
$strNoTags = strip_tags($str);
if (preg_match_all( '@(?<![.*">])\b(?:(?:https?|ftp|file)://|[a-z]\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]@i', $strNoTags, $matches)) {
foreach ($matches[0] as $match) {
$str = str_replace($match, "<a href=\"$match\">$match</a>", $str);
}
}
return $str;
}
What it does:
As it was commented, you could always try a HTML parser first to extract the text instead of strip_tags
.
Upvotes: 1