简体   繁体   中英

Skipping nodes when using DomXpath in PHP

I'm using a DomXpath query to find all HTML elements in a file that contain text that a user would see when the page is rendered by the browser.

The following lines of code do the trick pretty well:

$xpath = new DOMXpath($doc);
$results = $xpath->query("//text()");

However, there are some side effects. For example, if you have a

<style>

tag in your HTML it will read inside it and parse all CSS elements and I'd like to totally ignore it. I can't seem to find a way to form the query above to ignore one or more text element. Any help would be appreciated.

I found a way to avoid the <style> tag.

$results = $xpath->query("//body//text()")

This will ignore anything before the body of the HTML file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM