Replace URLs in text with HTML links

后端 未结 17 1742
栀梦
栀梦 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

    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 ;)

提交回复
热议问题