Replace URLs in text with HTML links

后端 未结 17 1991
栀梦
栀梦 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 12:02

    Here is something i found that is tried and tested

    function make_links_blank($text)
    {
      return  preg_replace(
         array(
           '/(?(?=]*>.+<\/a>)
                 (?:]*>.+<\/a>)
                 |
                 ([^="\']?)((?:https?|ftp|bf2|):\/\/[^<> \n\r]+)
             )/iex',
           '/]*)target="?[^"\']+"?/i',
           '/]+)>/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\\2\\3':'\\0'))",
           '',
           "stripslashes((strlen('\\2')>0?'\\1\\2\\3':'\\0'))",
           "stripslashes((strlen('\\2')>0?'\\0':'\\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.

提交回复
热议问题