2011-01-11 84 views
0

您好我想幫助的情況下,我有一個文件夾叫「滑梯」,我有它的多個text/html的文件,如: slide1.html slide2.html slide3.html 等等.....解析多個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行)的值作爲變量返回,以便我可以將它們放在我的代碼中進行佈局。

+0

你的問題是:

你可以在PHP中使用類似做到這一點? – 2011-01-11 18:33:04

+0

您是否可以更精確地瞭解您希望如何處理HTML?特別是,我不確定你的意思是「每條線的價值」。另外,你有一個首選的語言來寫這個嗎? – Karmastan 2011-01-11 18:33:49

回答

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行上。

+0

嘿@JMC那很完美。這正是我所期待的。感謝您的幫助。而且,HTML文件只有3行。 1行中的每個字段。 – 2011-01-11 21:40:34

0

你想要什麼語言? HTML不是一種編程語言。你也可以在Javascript中完成這項工作,因爲它沒有文件系統處理程序,在任何情況下幾乎肯定不會允許在服務器的目錄結構中徘徊。

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