PHP / RegEx - Convert URLs to links by detecting .com/.net/.org/.edu etc

后端 未结 3 1807
南笙
南笙 2020-12-11 10:23

I know there have been many questions asking for help converting URLs to clickable links in strings, but I haven\'t found quite what I\'m looking for.

I want to be a

相关标签:
3条回答
  • 2020-12-11 10:35

    Thanks a ton. I modified his final solution to allow all domains (.ca, .co.uk), not just the specified ones.

    $html = preg_replace_callback('#(\s|^)((https?://)?(\w|-)+(\.[a-z]{2,3})+(\:[0-9]+)?(?:/[^\s]*)?)(?=\s|\b)#is',
        create_function('$m', 'if (!preg_match("#^(https?://)#", $m[2])) return $m[1]."<a href=\"http://".$m[2]."\" target=\"blank\">".$m[2]."</a>"; else return $m[1]."<a href=\"".$m[2]."\" target=\"blank\">".$m[2]."</a>";'),
        $url);
    
    0 讨论(0)
  • 2020-12-11 10:45
    '/(http(s)?:\/\/)?[\w\/\.]+(\.((com)|(edu)|(net)|(org)))[\w\/]*/'
    

    That works for your examples. You might want to add extra characters support for "-", "&", "?", ":", etc in the last bracket.

    '/(http(s)?:\/\/)?[\w\/\.]+(\.((com)|(edu)|(net)|(org)))[\w\/\?=&-;]*/'
    

    This will support parameters and port numbers.

    eg.: www.foo.ca:8888/test?param1=val1&param2=val2

    0 讨论(0)
  • 2020-12-11 10:52

    You can use this regex:

    #(\s|^)((?:https?://)?\w+(?:\.\w+)+(?<=\.(net|org|edu|com))(?:/[^\s]*|))(?=\s|\b)#is
    

    Code:

    $arr = array(
    'http://www.domain.com/?foo=bar',
    'http://www.that"sallfolks.com',
    'This is really cool site: https://www.domain.net/ isn\'t it?',
    'http://subdomain.domain.org',
    'www.domain.com/folder',
    'Hello! You can visit vertigofx.com/mysite/rocks for some awesome pictures, or just go to vertigofx.com by itself',
    'subdomain.domain.net',
    'subdomain.domain.edu/folder/subfolder',
    'Hello! Check out my site at domain.net!',
    'welcome.to.computers',
    'Hello.Come visit oursite.com!',
    'foo.bar',
    'domain.com/folder',
    
    );
    foreach($arr as $url) {   
       $link = preg_replace_callback('#(\s|^)((?:https?://)?\w+(?:\.\w+)+(?<=\.(net|org|edu|com))(?:/[^\s]*|))(?=\s|\b)#is',
               create_function('$m', 'if (!preg_match("#^(https?://)#", $m[2]))
                   return $m[1]."<a href=\"http://".$m[2]."\">".$m[2]."</a>"; else return $m[1]."<a href=\"".$m[2]."\">".$m[2]."</a>";'),
               $url);
       echo $link . "\n";
    

    OUTPUT:

    <a href="http://www.domain.com/?foo=bar">http://www.domain.com/?foo=bar</a>
    http://www.that"sallfolks.com
    This is really cool site: <a href="https://www.domain.net">https://www.domain.net</a>/ isn't it?
    <a href="http://subdomain.domain.org">http://subdomain.domain.org</a>
    <a href="http://www.domain.com/folder">www.domain.com/folder</a>
    Hello! You can visit <a href="http://vertigofx.com/mysite/rocks">vertigofx.com/mysite/rocks</a> for some awesome pictures, or just go to <a href="http://vertigofx.com">vertigofx.com</a> by itself
    <a href="http://subdomain.domain.net">subdomain.domain.net</a>
    <a href="http://subdomain.domain.edu/folder/subfolder">subdomain.domain.edu/folder/subfolder</a>
    Hello! Check out my site at <a href="http://domain.net">domain.net</a>!
    welcome.to.computers
    Hello.Come visit <a href="http://oursite.com">oursite.com</a>!
    foo.bar
    <a href="http://domain.com/folder">domain.com/folder</a>
    

    PS: This regex only supports http and https scheme in URL. So eg: if you want to support ftp also then you need to modify the regex a little.

    0 讨论(0)
提交回复
热议问题