gpt4 book ai didi

php - 如何使用目录从 html 文件中获取标题 - php

转载 作者:搜寻专家 更新时间:2023-10-31 21:37:00 25 4
gpt4 key购买 nike

对 php 相当陌生,所以请多多包涵。我正在尝试弄清楚如何读取目录/文件夹并将文件名/路径和该文件的标题返回到 li 中。

$handle = opendir('.'); 
while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($filename, '.'), 1));
if($extension == 'html' || $extension == 'htm' || $extension == 'php'){
echo "<a href='".$filename."'><li class='"iso_block"'><span>"**Title Tag Here**"</span></li></a>";
}
}

^来自 miro 的代码(欢呼/感谢)

js.Fiddle 视觉链接:http://jsfiddle.net/AagGZ/547/

最佳答案

$handle = opendir('.');
$dom = new DOMDocument();

while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($file, '.'), 1));
if ($extension == 'html' || $extension == 'htm' || $extension == 'php') {
$title = 'Untitled Document';

if($dom->loadHTMLFile($urlpage)) {
$list = $dom->getElementsByTagName("title");
if ($list->length > 0) {
$title = $list->item(0)->textContent;
}
}

echo "<a href='$filename'><li class='iso_block'><span>$title</span></li></a>";
}
}

关于php - 如何使用目录从 html 文件中获取标题 - php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16763769/

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