regex problem in php

后端 未结 5 1523
情书的邮戳
情书的邮戳 2021-01-26 17:45
...

How to match the html inside(including)

in PHP?

I need a

5条回答
  •  抹茶落季
    2021-01-26 18:08

    Use DOM and DOMXPath instead of regex, you'll thank me for it:

    // something useful:
    function dumpDomNode ($node) {
        $temp = new DOMDocument();
        $temp->appendChild($node,true);
        return $temp->saveHTML();
    }
    
    $dom = new DOMDocument();
    $dom->loadHTML($html_string);
    
    $xpath-> new DOMXpath($dom);
    
    $elements = $xpath->query("*/div/[@class='begin']");
    
    foreach ($elements as $el) {
        echo dumpDomNode($el); // <-- or do something more useful with it
    }
    

    Trying this with regex will lead you down the path to insanity...

提交回复
热议问题