简体   繁体   English

PHP:将文本链接转换为锚标记

[英]PHP: Converting text links to anchor tags

I am pulling in RSS feeds and using DOMXPath to convert all existing anchor tags to custom tags that look like this for various reasons: 我正在使用RSS提要并使用DOMXPath将所有现有的锚标签转换为自定义标签,因为各种原因:

[webserviceLink]{$url}[/webserviceLink][webserviceLinkName]{$text}[/webserviceLinkName]

This works great, but I'd also like to covert all non-html text links to this same format, but am having some issues. 这很好用,但我也想将所有非HTML文本链接转换为相同的格式,但是我遇到了一些问题。

Here's my code for converting the text links: 这是我转换文本链接的代码:

$pattern = '(?xi)(?<![">])\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';

$desc = preg_replace_callback("#$pattern#i", function($matches)
{
  $input = $matches[0];
  $url = preg_match('!^https?://!i', $input) ? $input : "http://$input";

  if (strlen($input) > 20 && !strpos($input, " "))
    $input = substr($input, 0, 18)."... ";

  return "[webserviceLink]{$url}[/webserviceLink][webserviceLinkName]{$input}[/webserviceLinkName]";
}, $desc);

I'm not sure how to do the negative callback in this regex to check that the link I am converting is not in an existing html tag, like an img, or in my custom link tags above. 我不确定如何在此正则表达式中进行负回调,以检查我转换的链接是不是在现有的html标记中,如img,还是在我上面的自定义链接标记中。

I was able to use xpath to get this working. 我能够使用xpath来实现这一点。

$dom = new DOMDocument();
$dom->loadHTML(mb_convert_encoding($desc, 'HTML-ENTITIES', 'UTF-8'));
$xp = new DOMXPath($dom);

foreach ($xp->query('//text()[not(ancestor::a)]') as $node)
     {
        $pattern = '((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';

        $replaced = preg_replace_callback("#$pattern#i", function($matches)
        {
          $input = $matches[0];
          $url = preg_match('!^https?://!i', $input) ? $input : "http://$input";

          if (strlen($input) > 20 && !strpos($input, " "))
            $input = substr($input, 0, 18)."... ";

          return "<a href=\"{$url}\">{$input}</a>";
        }, $node->wholeText);

          $newNode  = $dom->createDocumentFragment();
          $newNode->appendXML($replaced);
          $node->parentNode->replaceChild($newNode, $node);
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM