jasonwisdom Posted September 25, 2008 Share Posted September 25, 2008 Hi, I am coding a website.. where users type in a message, and it displays for the public. Standard PHP 5.2.5. When the text contains a hyperlink, I want to wrap an <a href="link"> </a> around it. How do I detect such a thing....is there a regular expression I can use? Thanks for saving me time. Jason Link to comment https://forums.phpfreaks.com/topic/125824-when-displaying-text-how-do-i-recognize-a-hyperlink/ Share on other sites More sharing options...
discomatt Posted September 25, 2008 Share Posted September 25, 2008 From the PHP manual user contrib I thought that someone could use this hyperlink function. preg_replace is about 6 times faster than ereg_replace. I took the original example from the ereg_replace function page and modified so that it works perfect. I gave a comment of what it matches. One thing is that I added a space at the beginning so that only links that don't have <a href="" around them or anything else touching will be replaced. <?php function hyperlink(&$text) { // match protocol://address/path/file.extension?some=variable&another=asf% $text = preg_replace("/\s(([a-zA-Z]+:\/\/)([a-z][a-z0-9_\..-]*[a-z]{2,6})([a-zA-Z0-9\/*-?&%]*))\s/i", " <a href=\"$1\">$3</a> ", $text); // match www.something.domain/path/file.extension?some=variable&another=asf% $text = preg_replace("/\s(www\.([a-z][a-z0-9_\..-]*[a-z]{2,6})([a-zA-Z0-9\/*-?&%]*))\s/i", " <a href=\"http://$1\">$2</a> ", $text); return $text; } ?> Play around with it and see how it works. Courtesy of AmazingDiscoveries.org God bless, Iasmin Balaj Link to comment https://forums.phpfreaks.com/topic/125824-when-displaying-text-how-do-i-recognize-a-hyperlink/#findComment-650617 Share on other sites More sharing options...
Push Eject Posted September 25, 2008 Share Posted September 25, 2008 I just went through this. Perhaps this will help you: <?php $body = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $body); $body = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $body); ?> Link to comment https://forums.phpfreaks.com/topic/125824-when-displaying-text-how-do-i-recognize-a-hyperlink/#findComment-650618 Share on other sites More sharing options...
Push Eject Posted September 25, 2008 Share Posted September 25, 2008 Wow, Matt, you are FAST! Link to comment https://forums.phpfreaks.com/topic/125824-when-displaying-text-how-do-i-recognize-a-hyperlink/#findComment-650619 Share on other sites More sharing options...
jasonwisdom Posted September 25, 2008 Author Share Posted September 25, 2008 You BOTH are fast. Thank you!! I will play with both, a little later - other things are coming up. Thanks again. Jason Link to comment https://forums.phpfreaks.com/topic/125824-when-displaying-text-how-do-i-recognize-a-hyperlink/#findComment-650625 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.