2013-05-26 72 views
3

對於php來說是新鮮事物,所以請耐心等待。我想弄清楚如何讀取一個目錄/文件夾,並將文件名/路徑和文件的標題返回給li。如何從html文件中使用目錄獲得標題 - php

$handle = opendir('.'); 
while (false !== ($file = readdir($handle))){ 
    $extension = strtolower(substr(strrchr($filename, '.'), 1)); 
    if($extension == 'html' || $extension == 'htm' || $extension == 'php'){ 
     echo "<a href='".$filename."'><li class='"iso_block"'><span>"**Title Tag Here**"</span></li></a>"; 
    } 
} 

^從米羅代碼(歡呼聲/感謝)

js.Fiddle鏈接視覺:[`glob`] http://jsfiddle.net/AagGZ/547/

+1

你嘗試過(http://php.net /manual/en/function.glob.php)? –

+0

在dom中加載文件並搜索標題標籤(慢)或preg_match(「#(。*)#」,...)。 – bwoebi

+1

jsFiddle for testing php?你想要達到什麼樣的人? –

回答

1
$handle = opendir('.'); 
$dom = new DOMDocument(); 

while (false !== ($file = readdir($handle))){ 
    $extension = strtolower(substr(strrchr($file, '.'), 1)); 
    if ($extension == 'html' || $extension == 'htm' || $extension == 'php') { 
     $title = 'Untitled Document'; 

     if($dom->loadHTMLFile($urlpage)) { 
      $list = $dom->getElementsByTagName("title"); 
      if ($list->length > 0) { 
       $title = $list->item(0)->textContent; 
      } 
     } 

     echo "<a href='$filename'><li class='iso_block'><span>$title</span></li></a>"; 
    } 
} 
相關問題