gpt4 book ai didi

php - 如何使用 PHP(特别是 WordPress)提取页面标题

转载 作者:行者123 更新时间:2023-12-04 06:03:51 27 4
gpt4 key购买 nike

是否可以使用 PHP 从使用 PHP 的页面中提取标题(h1、h2 等),并将它们以无序列表的形式列在同一页面上?特定于 WordPress 的解决方案很好,但也欢迎使用通用的 PHP 解决方案。

编辑:我想要的是某种形式

h1
h2
h3
h3
h2
h3
h3

最佳答案

在 php 中,您可以使用 xml 操作:

http://www.php.net/manual/en/domdocument.getelementsbytagname.php

还没有测试过,但是 h1 是这样的:

$dom = new DOMDocument();

@$dom->loadHTML(file_get_contents('htmlfile.htm'))

$h1 = $dom->getElementsByTagName('h1');

foreach ( $h1 as $val ){
echo $val->property->__toString();
}

我不太确定这些函数,我无法测试(可能要到明天)...我从 http://br.php.net/manual/en/class.domelement.php#98851 得到了 tostring

关于php - 如何使用 PHP(特别是 WordPress)提取页面标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8620958/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com