【问题标题】:PHP regular expression to remove tags in HTML documentPHP正则表达式删除HTML文档中的标签
【发布时间】:2010-11-24 19:09:35
【问题描述】:

假设我有以下文字

..(content).............
<A HREF="http://foo.com/content" >blah blah blah </A>
...(continue content)...

我想删除链接并且我想删除标签(同时保留中间的文本)。如何使用正则表达式执行此操作(因为 URL 都会不同)

非常感谢

【问题讨论】:

标签: php regex preg-replace html-parsing


【解决方案1】:

这将删除所有标签:

preg_replace("/<.*?>/", "", $string);

这将只删除&lt;a&gt; 标签:

preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $string);

【讨论】:

  • 这不会清除所有标签吗?
  • 这不是要求的吗?
  • 完美!直接而严格。
【解决方案2】:

尽可能避免使用正则表达式,especially when processing xml。在这种情况下,您可以使用strip_tags()simplexml,具体取决于您的字符串。

【讨论】:

    【解决方案3】:
    <?php
    //example to extract the innerText from all anchors in a string
    include('simple_html_dom.php');
    
    $html = str_get_html('<A HREF="http://foo.com/content" >blah blah blah </A><A HREF="http://foo.com/content" >blah blah blah </A>');
    
    //print the text of each anchor    
    foreach($html->find('a') as $e) {
        echo $e->innerText;
    }
    ?>
    

    PHP Simple DOM Parser

    【讨论】:

      【解决方案4】:

      不漂亮,但可以:

      $data = str_replace('</a>', '', $data);
      $data = preg_replace('/<a[^>]+href[^>]+>/', '', $data);
      

      【讨论】:

      • strip_tags 在 HTML 格式良好时运行良好。我遇到了一个 HTML 文件的问题,其中属性缺少引号,这种方法很有效。谢谢!
      【解决方案5】:

      strip_tags()也可以用。

      请查看示例here

      【讨论】:

      • 欢迎来到 Stack Overflow!虽然这可能会回答问题,但it would be better 在此处包含答案的基本部分,并提供链接以供参考。
      • @senderle,我大体上同意你的观点,但这次不是“任何”外部页面,它是 PHP.net 的官方页面,描述了strip_tag 函数,无需在此处复制代码示例;)此答案已包含函数名称及其链接引用。
      【解决方案6】:

      我用它来用文本字符串替换锚点...

      function replaceAnchorsWithText($data) {
              $regex  = '/(<a\s*'; // Start of anchor tag
              $regex .= '(.*?)\s*'; // Any attributes or spaces that may or may not exist
              $regex .= 'href=[\'"]+?\s*(?P<link>\S+)\s*[\'"]+?'; // Grab the link
              $regex .= '\s*(.*?)\s*>\s*'; // Any attributes or spaces that may or may not exist before closing tag
              $regex .= '(?P<name>\S+)'; // Grab the name
              $regex .= '\s*<\/a>)/i'; // Any number of spaces between the closing anchor tag (case insensitive)
      
              if (is_array($data)) {
                  // This is what will replace the link (modify to you liking)
                  $data = "{$data['name']}({$data['link']})";
              }
              return preg_replace_callback($regex, array('self', 'replaceAnchorsWithText'), $data);
          }
      

      【讨论】:

        【解决方案7】:
        $pattern = '/href="([^"]*)"/';
        

        【讨论】:

          【解决方案8】:

          使用 str_replace

          【讨论】:

          • 他应该如何处理不同的href字符串?
          • (我不是投反对票的人,但似乎他不会解释为什么他投反对票,这没什么帮助,我可以补充一下,让我们猜猜为什么......)使用 str_replace,你不能指定一个“模式”,这是一个问题,因为 URL 可以更改;即使它没有改变,您也必须对 str_replace 使用两次调用:一次用于 openig 标记,一次用于结束标记,因为您想保留 beetween 的内容。
          猜你喜欢
          • 2011-04-16
          • 2019-11-25
          • 2017-08-16
          • 2012-01-26
          • 2023-01-25
          • 1970-01-01
          • 2010-10-20
          相关资源
          最近更新 更多