代码之家  ›  专栏  ›  技术社区  ›  Alex

PHP-Strings-删除带有特定类的HTML标记,包括其内容

  •  7
  • Alex  · 技术社区  · 15 年前

    我有这样一根线:

    <div class="container">
      <h3 class="hdr"> Text </h3>
      <div class="main">
        text
        <h3> text... </h3>
        ....
    
      </div>
    </div>
    

    如何使用尽可能少的代码删除带有.hdr类的H3标记?

    5 回复  |  直到 15 年前
        1
  •  22
  •   Daniel Egeberg    15 年前

    用作 小的 h3 标签 看起来,这就足够了:

    $html = preg_replace('#<h3 class="hdr">(.*?)</h3>#', '', $html);
    

    一般来说,使用regex解析HTML并不是一个好主意。

        2
  •  3
  •   Ben    15 年前

    像这样的东西是你要找的。。。

    $output = preg_replace("#<h3 class=\"hdr\">(.*?)</h3>#is", "", $input);
    

    在regex的末尾使用“is”,因为这会导致它不区分大小写,这更灵活。

        3
  •  0
  •   Alex    15 年前

    尝试preg\u匹配,然后按以下模式进行preg\u替换:

    /(<h3
    [\s]+
    [^>]*?
    class=[\"\'][^\"\']*?hdr[^\"\']*?[\"\']
    [^>]*?>
    [\s\S\d\D\w\W]*?
    <\/h3>)/i
    

    还没试过,可能需要调整。

    另一种方法是复制这个函数,如果可能的话,使用你的副本,不使用h3。

        4
  •  0
  •   Hissvard    5 年前

    通过Google偶然发现了这一点-对于其他使用regex解析HTML感到不舒服的人来说,下面是一个DOMDocument解决方案,我觉得这样做更安全:

    function removeTagByClass(string $html, string $className) {
        $dom = new \DOMDocument();
        $dom->loadHTML($html);
        $finder = new \DOMXPath($dom);
    
        $nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' {$className} ')]");
    
        foreach ($nodes as $node) {
            $node->parentNode->removeChild($node);
        }
    
        return $dom->saveHTML();
    }
    

    感谢 this other answer 用于XPath查询。

        5
  •  -1
  •   Jasmeen    10 年前

    $content=preg\u replace(“~(.*?~”,“”,$content);

    $content=preg\u replace('~[^ |]*?~',''',$content);

    即使中间有换行符,这种方法仍然有效,但如果不常用的符号位于中间,则会失败。有人知道更好的方法吗?

        6
  •  -1
  •   Jarnail S    7 年前

    如果上述解决方案不起作用,这将对某人有所帮助。它删除带有标记'-webkit overflow scrolling:touch;'的iframe和内容就像我说的:)

    RegEx或正则表达式是您想要删除的代码,PHP函数preg\u replace()将删除所有与之匹配的div或div,或者用其他内容替换它们。在下面的示例中,$incoming\u data是在删除元素之前放置所有内容的地方,$result是最终产品。基本上,我们告诉代码找到class=myclass的所有div,并用(nothing)替换它们。

    如何在PHP中按类删除div及其内容 把我的课改成你的课。

     $result = preg_replace('#<div class="myclass">(.*?)</div>#', ' ',
     $incoming_data);
    

    如何在PHP中按ID删除div及其内容

    $result=preg#u replace('#(.*?)#','',$传入的#数据);

    如果你的div有多个类? 只要把我的身份证换成你部门的身份证就行了。

    $result = preg_replace('#<div id="myid(.*?)</div>#', ' ', $incoming_data);
    or if div don’t have an ID, filter on the first class of the div like this.
    $result = preg_replace('#<div class="myclass(.*?)</div>#', ' ', $incoming_data);
    

    如何删除PHP中的所有标题 这是如何删除所有标题。

    $result = preg_replace('#<h1>(.*?)</h1>#', ' ', $incoming_data);
    and if the heading have a class, do something like this:
    $result = preg_replace('#<h1 class="myclass">(.*?)</h1>#', ' ', $incoming_data);
    

    http://www.lets-develop.com/html5-html-css-css3-php-wordpress-jquery-javascript-photoshop-illustrator-flash-tutorial/php-programming/remove-div-by-class-php-remove-div-contents/

    推荐文章