【问题标题】:Regex to remove external links except provided domain related links php正则表达式删除外部链接,除了提供的域相关链接 php
【发布时间】:2016-12-31 17:07:50
【问题描述】:

我希望正则表达式从我的内容中删除所有外部链接,只保留所提供域的链接。

例如。

$inputContent = 'Lorem Ipsum <a href="http://www.example1.com" target="_blank">http://www.example1.com</a> lorem ipsum dummy text <a href="http://www.mywebsite.com" target="_blank">http://www.mywebsite.com</a>';

预期输出:

$outputContent = 'Lorem Ipsum lorem ipsum dummy text <a href="http://www.mywebsite.com" target="_blank">http://www.mywebsite.com</a>';

尝试了这个解决方案,但它不起作用。

$pattern = '#<a [^>]*\bhref=([\'"])http.?://((?<!mywebsite)[^\'"])+\1 *>.*?</a>#i';  
$filteredString = preg_replace($pattern, '', $content);

【问题讨论】:

  • 尝试先检查您的正则表达式,您有 3 个未转义的分隔符。您可以使用此站点检查您的正则表达式一致性。 regex101.com
  • 您的正则表达式不考虑target=_blank 或任何其他属性。

标签: php regex


【解决方案1】:

尝试了这个解决方案,但它不起作用。

$pattern = '#<a [^>]*\bhref=([\'"])http.?://((?<!mywebsite)[^\'"])+\1 *>.*?</a>#i';

你很亲密。要使您的解决方案有效,请仅删除一个&gt;,即。 e.

  $pattern = '#<a [^>]*\bhref=([\'"])http.?://((?<!mywebsite)[^\'"])+\1 *.*?</a>#i';

【讨论】:

    【解决方案2】:

    正则表达式的解决方案:

    $inputContent = 'Lorem Ipsum <a href=\'http://www.example1.com\' target="_blank"><strong>http://www.example1.com</strong></a> lorem ipsum dummy text <a href="http://www.mywebsite.com" target="_blank">http://www.mywebsite.com</a>';  
    
    function callback($matches) {
        //print_r($matches);
    
        if (preg_match('#^https?://(www\.)?mywebsite\.com(/.+)?$#i', $matches[1])) {
            return '<a href="' . $matches[1] . '" target="_blank">' . $matches[2] . '</a>';
        }
    
        //return '';
        return $matches[2]; // or you can remove only the anchor and print the text only
    }
    
    $pattern = '#<a[^>]*href=[\'"]([^\'"]*)[\'"][^>]*>(((?!<a\s).)*)</a>#i';
    $filteredString = preg_replace_callback($pattern, 'callback', $inputContent);
    
    echo $filteredString;
    

    【讨论】:

      【解决方案3】:

      你真正需要的不是正则表达式。您正在解析 HTML 文档,因此您应该为它选择正确的工具:DOMDocument

      <?php
      
      $html = <<< HTML
      Lorem Ipsum <a href="http://www.example1.com" target="_blank">http://www.example1.com</a>
      lorem ipsum dummy text
      <a href="http://mywebsite.com" target="_blank">http://www.mywebsite.com</a>
      HTML;
      
      
      $dom = new \DOMDocument();
      $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED  | LIBXML_HTML_NODEFDTD);
      $xpath = new \DOMXPath($dom);
      
      $site = 'mywebsite.com';
      // Query all `a` tags that don't start with your website domain name
      $anchors = $xpath->query("//a[not(starts-with(@href,'http://{$site}')) and not(starts-with(@href,'http://www.{$site}'))]");
      
      foreach ($anchors as $anchor) {
          $anchor->parentNode->removeChild($anchor);
      }
      
      echo $dom->saveHTML();
      

      输出:

      <p>Lorem Ipsum 
      lorem ipsum dummy text
      <a href="http://mywebsite.com" target="_blank">http://www.mywebsite.com</a></p>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-23
        • 2010-10-23
        • 2010-10-31
        • 2017-04-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多