从 PHP Simple HTML DOM 获取所有标头标签


get all header tags from php simple html dom

我正在使用简单的html dom进行一些抓取,并想知道是否有办法一次点击获得所有H标签的集合 - 即H1 H2 H3等...

一些顺序的东西

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

然后,我还需要确切地知道它是哪个标签 - <H1> <H2>等。

任何帮助表示赞赏

你可以做类似的事情

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

尝试$xpath>query

例:

/* 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());