Log in

View Full Version : Text URL to hyperlink (ignore existing <img> and <a> tags)



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

Beverleyh
04-21-2011, 11:55 AM
I'm still curious to find a fix for the function above but I just wanted to let you know that I've found another way that suits my particular setup - my particular setup being that the URL needing to be made clickable is always on its own line, and has the text prefix 'Visit this link:'. So using that as an identifier, I can use the method below;


$handle = fopen('/path/to/file.txt', 'r');
$identifier = 'Visit this link:';
if ($handle) {
while (!feof($handle)) {
$text = fgets($handle);
if (is_integer(strpos($text,$identifier))) { // find lines with 'Visit this link:' with text URL to be made clickable
echo makeClickableLinks($text); // only apply makeClickableLinks() function to lines where identifier is found
} else {
echo $text;
}
}
fclose($handle);
}

james438
04-21-2011, 06:58 PM
$text=
"test 1: <a href=\"http://www.animeviews.com/article.php?ID=59\">http://www.animeviews.com/article.php?ID=59</a>. word.
<br>test 2: www.animeviews.com/article.php?ID=59. word.
<br>test 3: http://www.animeviews.com/article.php?ID=59. word.
<br>test 4: <a href=\"http://www.animeviews.com\">test 4</a>. word.
<br>test 5: <a href=\"www.animeviews.com\">www.animeviews.com</a> word.
<br>test 6: http://en.wikipedia.org/wiki/Portable_Network_Graphics word.";


<?php
$text = preg_replace('/(?<!http:\/\/|https:\/\/|\"|=|\'|\'>|\">)(www\..*?)(\s|\Z|\.\Z|\.\s|\<|\>|,)/i',"<a href=\"http://$1\">$1</a>$2",$text);
$text = preg_replace('/(?<!\"|=|\'|\'>|\">|site:)(https?:\/\/(www){0,1}.*?)(\s|\Z|\.\Z|\.\s|\<|\>|,)/i',"<a href=\"$1\">$1</a>$3",$text);
echo "$text";
?>

I have a small collection of PCRE. The one above is what I use for what you are describing. It has served me very well. Test 5, listed above is broken, but this is by design. It is not designed to fix broken hyperlinks, only to correctly hyperlink web addresses.

Beverleyh
04-21-2011, 07:45 PM
That's very helpful James, thank you :)

james438
04-21-2011, 07:54 PM
no problem. Glad I could help :)