Replace URLs in text with HTML links

后端 未结 17 1757
栀梦
栀梦 2020-11-22 11:27

Here is a design though: For example is I put a link such as

http://example.com

in textarea. How do I get PHP t

相关标签:
17条回答
  • 2020-11-22 11:50

    I've been using this function, it works for me

    function AutoLinkUrls($str,$popup = FALSE){
        if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches)){
            $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
            for ($i = 0; $i < count($matches['0']); $i++){
                $period = '';
                if (preg_match("|\.$|", $matches['6'][$i])){
                    $period = '.';
                    $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
                }
                $str = str_replace($matches['0'][$i],
                        $matches['1'][$i].'<a href="http'.
                        $matches['4'][$i].'://'.
                        $matches['5'][$i].
                        $matches['6'][$i].'"'.$pop.'>http'.
                        $matches['4'][$i].'://'.
                        $matches['5'][$i].
                        $matches['6'][$i].'</a>'.
                        $period, $str);
            }//end for
        }//end if
        return $str;
    }//end AutoLinkUrls
    

    All credits goes to - http://snipplr.com/view/68586/

    Enjoy!

    0 讨论(0)
  • 2020-11-22 11:50

    If you want to trust the IANA you can get your current list of offcially supported TLDs in use there like:

      $validTLDs = 
    explode("\n", file_get_contents('http://data.iana.org/TLD/tlds-alpha-by-domain.txt')); //get the official list of valid tlds
      array_shift($validTLDs); //throw away first line containing meta data
      array_pop($validTLDs); //throw away last element which is empty
    

    Makes Søren Løvborg's solution #2 a bit less verbose and spares you the hassle of updating the list, nowadays new tlds are thrown out so carelessly ;)

    0 讨论(0)
  • 2020-11-22 11:54

    You guyz are talking way to advance and complex stuff which is good for some situation, but mostly we need a simple careless solution. How about simply this?

    preg_replace('/(http[s]{0,1}\:\/\/\S{4,})\s{0,}/ims', '<a href="$1" target="_blank">$1</a> ', $text_msg);
    

    Just try it and let me know what crazy url it doesnt satisfy.

    0 讨论(0)
  • 2020-11-22 11:57

    This should get your twitter handle without touching on your email /(?<=^|(?<=[^a-zA-Z0-9-.]))@([A-Za-z]+[A-Za-z0-9]+)/i

    0 讨论(0)
  • 2020-11-22 11:59

    This is just a variation of the solution posted by Dharmendra Jadon, so if you like it up vote his instead!

    I just added a parameter to make opening the link in a new window (target="_blank") optional, as I saw this in some of the other solutions and liked the flexibility:

    function MakeUrls($str, $popup = FALSE)
    {
        $find=array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`si','`((?<!//)(www\.\S+[[:alnum:]]/?))`si');
    
        $replace=array('<a href="$1"' . ($popup ? ' target="_blank"' : '') . '>$1</a>', '<a href="http://$1"' . ($popup ? ' target="_blank"' : '') . '>$1</a>');
    
        return preg_replace($find,$replace,$str);
    }
    
    0 讨论(0)
  • 2020-11-22 12:02

    Here is something i found that is tried and tested

    function make_links_blank($text)
    {
      return  preg_replace(
         array(
           '/(?(?=<a[^>]*>.+<\/a>)
                 (?:<a[^>]*>.+<\/a>)
                 |
                 ([^="\']?)((?:https?|ftp|bf2|):\/\/[^<> \n\r]+)
             )/iex',
           '/<a([^>]*)target="?[^"\']+"?/i',
           '/<a([^>]+)>/i',
           '/(^|\s)(www.[^<> \n\r]+)/iex',
           '/(([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-]+)
           (\\.[A-Za-z0-9-]+)*)/iex'
           ),
         array(
           "stripslashes((strlen('\\2')>0?'\\1<a href=\"\\2\">\\2</a>\\3':'\\0'))",
           '<a\\1',
           '<a\\1 target="_blank">',
           "stripslashes((strlen('\\2')>0?'\\1<a href=\"http://\\2\">\\2</a>\\3':'\\0'))",
           "stripslashes((strlen('\\2')>0?'<a href=\"mailto:\\0\">\\0</a>':'\\0'))"
           ),
           $text
       );
    }
    

    It works for me. And it works for emails and URL's, Sorry to answer my own question. :(

    But this one is the only that works

    Here is the link where i found it : http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_21878567.html

    Sry in advance for it being a experts-exchange.

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