Need help to come up with regex to turn this into a link
I need to turn the following into a link:
<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> />
Here is the regex I currently use that doesn't work:
$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);
Hike description is the text I need to make into a link.
In the edited question this is not really clear any more, but from the original one I guess that the whole problem here is that the link ends with <br
, which is not legal in HTML.
Try using htmlspecialchars on the $hike_description.
try trimming first the "/>"
$hike_description="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 />";
$hike_description =trim($hike_description,"/>");
$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);
精彩评论