squiblo Posted April 26, 2010 Share Posted April 26, 2010 I've Googled but cannot find anything, I'm sure it is out there but I'm looking in the wrong places. Lets say I have.. $string = "visit my website www.domain.com"; How can I put the url into a variable like.. $url = "www.domain.com"; and come up with something like.. $new_string = "visit my website <a href='http://" .$url. "'>" .$url. "</a>"; Thanks Quote Link to comment https://forums.phpfreaks.com/topic/199790-selecting-words-in-string-that-begin-with-www/ Share on other sites More sharing options...
litebearer Posted April 26, 2010 Share Posted April 26, 2010 as a start - http://www.php.net/manual/en/function.stristr.php then - http://www.php.net/manual/en/function.substr.php or - http://www.php.net/manual/en/function.preg-match.php Quote Link to comment https://forums.phpfreaks.com/topic/199790-selecting-words-in-string-that-begin-with-www/#findComment-1048693 Share on other sites More sharing options...
de.monkeyz Posted April 26, 2010 Share Posted April 26, 2010 I just googled "php auto link" and found this snippet function make_clickable($text) { $ret = ' ' . $text; $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", $ret); $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" >\\2</a>'", $ret); $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret); $ret = substr($ret, 1); return($ret); } Might be what you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/199790-selecting-words-in-string-that-begin-with-www/#findComment-1048694 Share on other sites More sharing options...
andrewgauger Posted April 26, 2010 Share Posted April 26, 2010 Ok, so I am going into uncharted territory with this one, but it looks like you need to mach the top level domain and anything preceding up to whitespace, so what I would do is $url=array(); $string="this is www.mydomain.com and so is www.mydomain.net but I don't own telnet.superuser.com"; preg_match_all("/(\S+\.com|\.net|\.info|\.biz|\.org|\.gov|\.uk|\.edu)/" , $string, $url); print_r($url); But you should check out http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains and possibly add some of the other ones you want. At least there are fewer top level domains than there are sub domains (because matching www. is not sufficient) Quote Link to comment https://forums.phpfreaks.com/topic/199790-selecting-words-in-string-that-begin-with-www/#findComment-1048714 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.