(轉換H1至H2,H2遞歸H3)字符串我知道我們可以用字符串加載到降級標題標籤使用PHP
$doc = DOMDocument::loadXML($xml_str);
獲得標題,然後得到像H1標籤所以:
$list = $doc->getElementsByTagName("h1");
for ($i = 0; $i < $list->length; $i++) {
print($list->item($i)->nodeValue . "<br/>\n");
}
如果我想改變這些H1爲H2s我有點失落。我已閱讀appendChild()
,但這會讓事情變得非常混亂。有沒有一種方法來遞歸降低包含html的字符串中的標題標籤?該方法將接受以下參數:
function demoteHeadings($xml_string, $top_level='H2'){
//if string's highest heading is more than $top_level,
//we demote all headings in this html by 1 level. i.e. if
//h1 is found, all h1s, h2s and so on are demoted one level -
//and we recursively call this function again;
if($top_level_in_xml > $top_level) demoteHeadings($output, $top_level);
}
我希望我說得通。我試圖實現的是自動解析我的客戶在他們的CMS中輸入的標題......他們在文章中使用H1,而標題已經是h1。有時候,還有一個頁面標題是h1,它在整個頁面上真正地混淆了結構。
感謝。有時候,當你認爲太困難時,解決方案可能太簡單而無法解決。謝謝! – Vik