2013-01-19 55 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