使用 php 在字符串中降级标题标签(递归地将 h1 转换为 h2,将 h2 转换为 h3)


Demote heading tags (convert h1 to h2, h2 to h3 recursively)in string using 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 更改为 H2,我有点迷茫。我读过关于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,这确实弄乱了整个页面的结构。

只使用 str_ireplace() 不是更简单吗

$content = str_ireplace(array('<h1>','</h1>'),array('<h2>','</h2>'),$input);

此解决方案将执行简单的搜索和替换以降级标题标签。例如,它会将<h1>更改为<h2><h2>更改为<h3>,依此类推。

更强大的解决方案将使用DOMDocument但我在堆栈溢出的任何地方都找不到有效的解决方案。我决定不花时间以最精确或"正确"的方式解决这个问题,因为这个解决方案在 99% 的情况下可能已经足够好了。

for ($i = 1; $i <= 5; $i++) {
    $html = str_ireplace("<h" . $i, "<h" . $i . "_replace", $html);
    $html = str_ireplace("</h" . $i . ">", "</h" . $i . "_replace" . ">", $html);
}
for ($i = 1; $i <= 5; $i++) {
    $html = str_ireplace("<h" . $i . "_replace", "<h" . ($i + 1), $html);
    $html = str_ireplace("</h" . $i . "_replace" . ">", "</h" . ($i + 1) . ">", $html);
}

由于文章将位于更深的容器中,因此适当地设置它们的样式应该不是问题。但是,我认为您已经考虑过

我会因为建议正则表达式来解析 HTML 而被告知......但是由于您的客户使用 CMS 输入 HTML,我收集输入的语法非常可靠并且元素没有属性,那么为什么不简单地str_replace呢?

啊,劳伦斯打败了我

str_ireplace解决方案的另一种变体,但更健壮一些(考虑到h1到h100)

function demoteHtmlHeaderTags($html)
{
        $originalHeaderTags = [];
        $demotedHeaderTags = [];
        foreach(range(100, 1) as $index)
        {
            $originalHeaderTags[] = '<h' . $index .'>';
            $originalHeaderTags[] = '</h' . $index . '>';
            $demotedHeaderTags[] = '<h' . ($index + 1) . '>';
            $demotedHeaderTags[] = '</h' . ($index + 1) . '>';
        }
        return str_ireplace($originalHeaderTags, $demotedHeaderTags, $html);
}