Jump to content

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)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.