Jump to content

selecting words in string that begin with www.


squiblo

Recommended Posts

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

 

 

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.

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.