我在主根目录中有一个文件夹,新闻。在该文件夹中,我有10个页面,每个页面都有一个唯一的页面标题:
/news/Today-is-cold-outside.php title =今天很冷
/news/Watch-out-for-the-smelly-frogs.php title =臭臭蟾蜍
我可以让下面的代码工作,以便它将获取每个页面并提供每个页面的链接,页面名称为$ file,例如:Today-is-cold-outside.php,而不是$ title =今天是冷。
如何让页面显示每个链接$ title的特定页面标题?
我知道我在下面列出了$ file而不是链接中的$ title,我把它放在那里,这样你就可以看到我想要显示标题的位置,如果你测试它,它会告诉你它是从目录/文件夹新闻中获取$ file = pages。
在此先感谢,我已经为此工作了2天,无法找到解决方案。
<?php
$handle = opendir('news');
$dom = new DOMDocument();
while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($file, '.'), 1));
if ($extension == 'html' || $extension == 'htm' || $extension == 'php') {
if($dom->loadHTMLFile($urlpage)) {
$list = $dom->getElementsByTagName("title");
if ($list->length > 0) {
$title = $list->item(0)->textContent;
}
}
echo "<a href='news/$file'>$file</a>";
}
}
?>
答案 0 :(得分:1)
尝试nodeValue
npm install whatwg-fetch@^1.0.0 --save
变化
$title = $list->item(0)->nodeValue
和
$dom->loadHTMLFile($file)
答案 1 :(得分:0)
如果$ file = Today-is-cold-outside.php
你想要$ title =今天是冷的
这样做:
$title = $file;
$title = str_replace('-',' ',$title);
$title = str_replace('.php','',$title);
echo $title;
这会将破折号转换为空格,并删除.php扩展名。
如果您喜欢我的回答,我也可以将每个单词从小写转换为大写,如果需要,还可以将单词的最大数量限制为3。
试试这个:
<?php
$handle = opendir('news');
$dom = new DOMDocument();
while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($file, '.'), 1));
if ($extension == 'html' || $extension == 'htm' || $extension == 'php') {
if($dom->loadHTMLFile($urlpage)) {
$list = $dom->getElementsByTagName("title");
if ($list->length > 0) {
$title = $list->item(0)->textContent;
}
}
$title = $file;
$title = str_replace('-',' ',$title);
$title = str_replace('.php','',$title);
echo '<a href="blog/'.$file.'">';
echo $title;
echo '</a>';
}
}
?>
<?php
$handle = opendir('blog');
$dom = new DOMDocument();
while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($file, '.'), 1));
if ($extension == 'html' || $extension == 'htm' || $extension == 'php') {
if($dom->loadHTMLFile('blog/'.$file)) {
$list = $dom->getElementsByTagName("title");
if ($list->length > 0) {
$title = $list->item(0)->textContent;
}
}
echo "<a href='blog/$file'>$title</a><br/>";
}
}
?>
<?php
$title = $file;
$title = str_replace('-',' ',$title);
$title = str_replace('.php','',$title);
echo '<a href="blog/'.$file.'">';
echo $title;
echo '</a>'; ?>
答案 2 :(得分:0)
<?php
$handle = opendir('news');
$dom = new DOMDocument();
while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($file, '.'), 1));
if ($extension == 'html' || $extension == 'htm' || $extension == 'php') {
if($dom->loadHTMLFile('news/'.$file)) {
$list = $dom->getElementsByTagName("title");
if ($list->length > 0) {
$title = $list->item(0)->textContent;
}
}
echo "<a href='news/$file'>$title</a><br/>";
}
}
?>
答案 3 :(得分:0)
<?php
$handle = opendir('news');
$dom = new DOMDocument();
$title = '';
while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($file, '.'), 1));
if ($extension == 'html' || $extension == 'htm' || $extension == 'php') {
if($dom->loadHTMLFile('news/'.$file)) {
$list = $dom->getElementsByTagName("title");
if ($list->length > 0) {
$title = $list->item(0)->textContent;
}
}
echo "<a href='news/$file'>$file</a>";
echo $title;
}
}
?>
我的目录名是&#34; news&#34;