I have this regex code:
$hike_description = nl2br($hike_description);
//$hike_description = str_replace(array('\n','\r'),'',$hike_description);
//Convert all urls to links
$hike_description = preg_replace('#([\s|^])(www)#i', '$1http://$2', $hike_description);
$pattern = '#((http|https|ftp|telnet|news|gopher|file|wais):\/\/[^\s]+)#i';
$replacement = '<a href="$1" target="_blank">$1</a>';
$hike_description = preg_replace($pattern, $replacement, $hike_description);
It has worked 100% of the time….until now.
In this page the code didn’t work for the first time. Specifically, it didn’t place the ” at the end of the link so the link kept going down the rest of the page:
http://www.comehike.com/hikes/scheduled_hike.php?hike_id=209
You see how the link from the middle of the page extends all the way to the bottom and is not closed?
Any clue why that might have happened this time and not any other time?
Thanks!
ps – this is the HTML that ends up on the page:
<a href="http://maps.google.com/maps?um=1&ie=UTF-8&q=little+river+canyon+center&fb=1&gl=us&hq=little+river+canyon+center&hnear=0x888a614b2e7272e5%3A0x913a5fafeec714d6%2CCentre%2C+AL&ei=GBsFTtedF8vUgAfex6zNAQ&sa=X&oi=local_group&ct=image&ved=0CAQQtgM<br" target="_blank">http://maps.google.com/maps?um=1&ie=UTF-8&q=little+river+canyon+center&fb=1&gl=us&hq=little+river+canyon+center&hnear=0x888a614b2e7272e5%3A0x913a5fafeec714d6%2CCentre%2C+AL&ei=GBsFTtedF8vUgAfex6zNAQ&sa=X&oi=local_group&ct=image&ved=0CAQQtgM<br</a> />
Your regular expression expects the URL to include all characters up to the next space character. Immediately following the URL was an html
<br />tag and the next space seen by your regex was the space before the/>in that tag. So it thought the URL also included the<brat the beginning of that tag. When the closing</a>was added, it was placed inside a<br />tag and therefore was invalidated.For a quick fix to this specific problem, try changing your regex to look for the
<character as well as a space to be a URL terminator:EDIT I think this is the real source of the problem:
Another possibility is to call
nl2br()after doing the text replacement rather than before. That<br />tag might have been a newline character beforehand. The newline would have been interpreted as a space by your regex and the regex replacement wouldn’t have choked.