Strip Tags and everything in between

前端 未结 5 1327
南方客
南方客 2020-11-29 08:30

How can i strip

including this content

I know you can use strip tags to remove the tags, but i want everything in between gone as w

5条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 09:16

    As you’re dealing with HTML, you should use an HTML parser to process it correctly. You can use PHP’s DOMDocument and query the elements with DOMXPath, e.g.:

    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $xpath = new DOMXPath($doc);
    foreach ($xpath->query('//h1') as $node) {
        $node->parentNode->removeChild($node);
    }
    $html = $doc->saveHTML();
    

提交回复
热议问题