我正在使用简单的html dom进行一些抓取操作,并想知道是否有一种方法可以一次获取所有H标签的集合 - 即H1 H2 H3等等...
类似于以下代码:
我需要知道确切的标签是哪一个 -
类似于以下代码:
$HTags = $html->find("h*");
我需要知道确切的标签是哪一个 -
<H1> <H2>
等等。任何帮助都将不胜感激。$HTags = $html->find("h*");
<H1> <H2>
等等。任何帮助都将不胜感激。foreach($html->find('h1,h2,h3') as $element){
/* 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());