Beverleyh
04-21-2011, 10:01 AM
I've been using this function to convert URLs from a text file to clickable links, but it also converts the URL in existing <img> and <a> tags too.
function makeClickableLinks($text) {
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $text);
$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $text);
return $text;
}
I'm not fully understanding patterns in PHP yet so please could somebody help me convert the above function to ignore the URL if its already inside an <img> or <a> tag and only convert the URL if its floating loose in the text.
If it helps aswell, the existing image tags always look something like this (no alt, height, width, style, class or id);
<img src="http://www.website.com/mypic.jpg">
And the existing anchors always look something like this (no title, style, class or id);
<a href="http://www.website.com/page.html">Some Text</a>
Fixed values are in blue. Both may or may not have the www. in the URL.
Hope that's enough info
function makeClickableLinks($text) {
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $text);
$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $text);
return $text;
}
I'm not fully understanding patterns in PHP yet so please could somebody help me convert the above function to ignore the URL if its already inside an <img> or <a> tag and only convert the URL if its floating loose in the text.
If it helps aswell, the existing image tags always look something like this (no alt, height, width, style, class or id);
<img src="http://www.website.com/mypic.jpg">
And the existing anchors always look something like this (no title, style, class or id);
<a href="http://www.website.com/page.html">Some Text</a>
Fixed values are in blue. Both may or may not have the www. in the URL.
Hope that's enough info