0

我有一个包含 5 页的文件夹,我想获取每个页面的链接、创建日期和标题。

现在我这样做如下:

<?php
    function getActus()
    {
        return array(

                $message1 = array(
                    'date'  => filectime("news/news1.php"),
                    'titre' => "News 1",
                    'url'   => "/Serie_4/ex3/news/news1.php"
                 ),

                $message2 = array(
                    'date'  => filectime("news/news2.php"),
                    'titre' => "News 2",
                    'url'   => "/Serie_4/ex3/news/news2.php"
                 ),

                $message3 = array(
                    'date'  => filectime("news/news3.php"),
                    'titre' => "News 3",
                    'url'   => "/Serie_4/ex3/news/news3.php"
                 ),

                $message4 = array(
                    'date'  => filectime("news/news4.php"),
                    'titre' => "News 4",
                    'url'   => "/Serie_4/ex3/news/news4.php"
                 ),

                $message5 = array(
                    'date'  => filectime("news/news5.php"),
                    'titre' => "News 5",
                    'url'   => "/Serie_4/ex3/news/news5.php"
                 )
            );
    }
?>

我想动态地这样做,因为有时我不知道该文件夹上有多少页面。

如何获取某个文件夹中的文件。

我还想获得每一页的标题,我的意思是标题介于<title>和之间</title>

4

1 回答 1

0

要检索目录中的文件列表,请查看: http: //php.net/manual/en/function.dir.php

检索每个文件的修改时间:http: //php.net/manual/en/function.filemtime.php

要从页面中提取标题:http://stackoverflow.com/questions/13510124/regular-expression-to-get-page-title

我更喜欢asad的答案:

$doc = new DOMDocument();
$doc->loadHTML($html);
$titles = $doc->getElementsByTagName("title");
echo $titles->item[0]->nodeValue;

但你会想从文件加载,而不是字符串:http://php.net/manual/en/domdocument.loadhtmlfile.php

于 2013-11-10T16:22:49.720 回答