2013-01-19 281 views
-1

我有一個代碼正在工作,因爲我想從1個問題中分離出來。從文件中刪除擴展名

你可以在這裏看到結果http://test.whatanswered.com/health/what-can-a-first-aider-do.php在「相關文章」下面顯示死鏈接。

以下是應該顯示的HTML。

<p><a href="../health/name-of-the-page.php">Name of the page</a></p> 

我想剝離破折號和PHP作爲上述「頁面名稱」,但它也剝去從URL破折號和PHP。

的代碼是:

<?php 

if ($handle = opendir('health')) { 
    $fileTab = array(); 
    while (false !== ($file = readdir($handle))) { 
     if ($file != "." && $file != "..") { 
      $fileTab[$file] = strtr(pathinfo($file, PATHINFO_FILENAME), '-', ' '); 
     } 
    } 
    closedir($handle); 
    shuffle($fileTab); 
    foreach(array_slice($fileTab, 0, 10) as $file => $health) { 
     $thelist .= '<p><a href="../health/'.$file.'">'.$health.'</a></p>'; 
    } 
} 
?> 
<?=$thelist?> 
+0

pathinfo($ myFile,PATHINFO_FILENAME)和str_replace()....閱讀說明書有幫助 –

+2

對不起,但我不明白你在問什麼。你能否以更清楚的方式嘗試措辭這個問題? – Charles

回答

1

我已經重構你的代碼一點 - 在$fileTab陣列現在只存儲文件名,這轉化爲標題顯示的點發生:

if ($handle = opendir('health')) { 
    $fileTab = array(); 
    while (false !== ($file = readdir($handle))) { 
     if ($file != "." && $file != "..") { 
      $fileTab[] = $file; 
     } 
    } 
    closedir($handle); 
    shuffle($fileTab); 

    foreach(array_slice($fileTab, 0, 10) as $file) { 
     $title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME)); 
     $thelist .= '<p><a href="../health/'.$file.'">'.$title.'</a></p>'; 
    } 
} 
+0

感謝您的嘗試,但仍然沒有運氣:[鏈接] http://test.whatanswered.com/health/what-c​​an-a-first-aider-do.php – mally

+0

我改寫了我的答案 - 請再試一次 –

+0

太棒了除了我不得不刪除=> $健康,因爲它似乎顯示一個數字列表。非常感謝 – mally