获取某些文件夹中的PHP文件

时间:2013-11-10 16:09:56

标签: php

我有一个包含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>之间的标题。

1 个答案:

答案 0 :(得分: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的anwser:

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

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