简体   繁体   中英

get all header tags from php simple html dom

I am using simple html dom to do some scraping and would like to know if there is a way to get a collection of all H tags in one hit - that is H1 H2 H3 etc...

Something of the order of

$HTags = $html->find("h*");

I would then also need to know exactly which tag it was - <H1> <H2> etc..

Any help appreciated

你可以做类似的事情

foreach($html->find('h1,h2,h3') as $element){

Try $xpath->query

Example:

/* The following example finds <h1> and <h2> tags in a html String and sets id to it. The html-code will be printed.*/
$html = "<h2>test2</h2><h1>test1</h1><h3>test3</h3>";
$dom = new DOMDocument();
@$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($dom);
$htags = $xpath->query('//h1 | //h2');
foreach($htags as $htag)
    $htag->setAttribute('id', 'test');

echo htmlentities($dom->saveHTML());

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