解析多个html /文本文件

时间:2011-01-11 18:29:56

标签: html parsing function

您好我需要帮助,我有一个名为'slide'的文件夹,我有多个text / html文件,如: slide1.html slide2.html slide3.html 等等......

这些文件的结构如下:

<h2>Title of the Slide</h2>
<p><a href="http://mydomain.com"><img src="tick_icon.jpg" width="227" height="227" alt="icon" longdesc="http://longdescription" /></a></p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

3个属性标题,图像和描述。每行一个。

我有10到12个像这样的文件。我想要一个函数,它将循环并解析名为'slides'的文件夹中的所有这些文件,并将每行(3行)的值作为变量返回,以便我可以将它们放在我的代码中进行布局。

2 个答案:

答案 0 :(得分:1)

您可以使用

foreach(glob('slides/*.html') as $fileName) {
    $fname = basename( $fileName );
    $curArr = file($fname);
    $slides[$fname ]['title'] = $curArr[0];
    $slides[$fname ]['image-links'] = $curArr[1];
    $slides[$fname ]['description'] = $curArr[2];
}

您最终会得到一个大的$slides数组,其中包含文件名作为键和3个子键,titleimage-linksdescription。这假设每个“幻灯片”都有扩展名.html,每张幻灯片的内容最终都是3行。

答案 1 :(得分:0)

你想要什么语言? HTML不是一种编程语言。您也无法在Javascript中实现此目的,因为它没有文件系统处理例程,并且几乎肯定不会允许在任何情况下都在服务器的目录结构周围。

您可以使用以下内容在PHP中完成此操作:

<?php
    $filelist = glob("/path/to/files/slide*.html");
    foreach($filelist as $file) {
        echo <<<EOL
<a href="/url/to/files/$file">$file</a><br />
EOL
}
?>