Q: How do I parse HTML code stored in a PHP variable to extract the text between headings, without using regular expressions?
A: Use PHP Document Object Model:
$DOM = new DOMDocument; $DOM->loadHTML($html); $items = $DOM->getElementsByTagName('h1'); for ($i = 0; $i < $items->length; $i++) echo $items->item($i)->nodeValue . "<br/>";
If you want the content between headings, use this regex:
echo preg_replace("#<h1.*?>.*?</h1>#", "", $html);
The above is the detailed content of How to Parse HTML in PHP to Extract Text Between Headings Without Regular Expressions?. For more information, please follow other related articles on the PHP Chinese website!