隐藏用户输入中的链接


Hide links in user input

我似乎找不到这个问题的答案;我有一个文本框,用户可以在其中书写,如果他们写 www.something.com 或类似的东西,我想替换他们的文本。我想用文本"网站"替换它。

到目前为止,我找到的最接近的答案是关于将纯文本转换为类似scinario中的链接的答案。我想知道我是否可以转身完成我的任务,但我的 php 不是很好!我只是有点猜测...然后有点卡住了。任何帮助或更时尚的方式,非常感谢!

function remove_links {
$validTlds = array_fill_keys(explode(" ", ".aero .asia .biz .cat .com .coop .edu .gov .info .int .jobs .mil .mobi .museum .name .net .org .pro .tel .travel .ac .ad .ae .af .ag .ai .al .am .an .ao .aq .ar .as .at .au .aw .ax .az .ba .bb .bd .be .bf .bg .bh .bi .bj .bm .bn .bo .br .bs .bt .bv .bw .by .bz .ca .cc .cd .cf .cg .ch .ci .ck .cl .cm .cn .co .cr .cu .cv .cx .cy .cz .de .dj .dk .dm .do .dz .ec .ee .eg .er .es .et .eu .fi .fj .fk .fm .fo .fr .ga .gb .gd .ge .gf .gg .gh .gi .gl .gm .gn .gp .gq .gr .gs .gt .gu .gw .gy .hk .hm .hn .hr .ht .hu .id .ie .il .im .in .io .iq .ir .is .it .je .jm .jo .jp .ke .kg .kh .ki .km .kn .kp .kr .kw .ky .kz .la .lb .lc .li .lk .lr .ls .lt .lu .lv .ly .ma .mc .md .me .mg .mh .mk .ml .mm .mn .mo .mp .mq .mr .ms .mt .mu .mv .mw .mx .my .mz .na .nc .ne .nf .ng .ni .nl .no .np .nr .nu .nz .om .pa .pe .pf .pg .ph .pk .pl .pm .pn .pr .ps .pt .pw .py .qa .re .ro .rs .ru .rw .sa .sb .sc .sd .se .sg .sh .si .sj .sk .sl .sm .sn .so .sr .st .su .sv .sy .sz .tc .td .tf .tg .th .tj .tk .tl .tm .tn .to .tp .tr .tt .tv .tw .tz .ua .ug .uk .us .uy .uz .va .vc .ve .vg .vi .vn .vu .wf .ws .ye .yt .yu .za .zm .zw .xn--0zwm56d .xn--11b5bs3a9aj6g .xn--80akhbyknj4f .xn--9t4b11yi5a .xn--deba0ad .xn--g6w251d .xn--hgbk6aj7f53bba .xn--hlcj6aya9esc7a .xn--jxalpdlp .xn--kgbechtv .xn--zckzah .arpa"), true);
  $position = 0;
  while (preg_match("{''b$rexProtocol$rexDomain$rexPort$rexPath$rexQuery$rexFragment(?=[?.!,;:'"]?('s|$))}", $text, &$match, PREG_OFFSET_CAPTURE, $position))
  {
      list($url, $urlPosition) = $match[0];
      // the text leading up to the URL.
      $pre = (htmlspecialchars(substr($text, $position, $urlPosition - $position)));
      $domain = $match[2][0];
      $port   = $match[3][0];
      $path   = $match[4][0];
      // Check if the TLD is valid - or that $domain is an IP address.
      $tld = strtolower(strrchr($domain, '.'));
      if (preg_match('{'.[0-9]{1,3}}', $tld) || isset($validTlds[$tld]))
      {
          // Prepend http:// if no protocol specified
          $completeUrl = $match[1][0] ? $url : "http://$url";
          // ------------ now I'm a bit stuck! How to modify beyond here? ------------------
          printf('<a href="%s">%s</a>', htmlspecialchars($completeUrl), htmlspecialchars("$domain$port$path"));
      }
      else
      {
          // Not a valid URL.
          print(htmlspecialchars($url));
      }
      // Continue text parsing from after the URL.
      $position = $urlPosition + strlen($url);
  }
  // Print the remainder of the text.
  print(htmlspecialchars(substr($text, $position)));
}

为什么不使用 filter_var() 和 FILTER_VALIDATE_URL(手动)?

<?php
$url = 'http://google.com';
//$url = 'invalid url www.google.com';
if (filter_var($url, FILTER_VALIDATE_URL)) {
    echo '<a href="'.$url.'">Link</a>';
} else {
    echo 'Not a url.';
}