簡體   English   中英

從 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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM