Php 从文件中删除扩展名

Php 从文件中删除扩展名,php,Php,我有一个代码,是工作,因为我希望它除了1个问题 你可以在右下方的“相关文章”中看到结果,显示死链接 以下是应显示的HTML <p><a href="../health/name-of-the-page.php">Name of the page</a></p> 我想去掉上面“页面名称”中的破折号和php,但它也会从url中去掉破折号和php 代码是: <?php if ($handle = opendir('health')) {

我有一个代码,是工作,因为我希望它除了1个问题

你可以在右下方的“相关文章”中看到结果,显示死链接

以下是应显示的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?> 

我对您的代码进行了一些重构-现在,
$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>';
    }
}
if($handle=opendir('health')){
$fileTab=array();
while(false!=($file=readdir($handle))){
如果($file!=“&&&$file!=”){
$fileTab[]=$file;
}
}
closedir($handle);
洗牌($fileTab);
foreach(数组_切片($fileTab,0,10)作为$file){
$title=str_replace('-','',pathinfo($file,pathinfo_FILENAME));
$thelist.='

'; } }
pathinfo($myFile,pathinfo\u FILENAME)和str\u replace()。。。。阅读手册,但我真的不明白你在问什么。你能试着用一种更清晰的方式来表达这个问题吗?谢谢你的尝试,但仍然没有成功:[link]我已经重写了我的答案-请尝试一下,除了我必须删除=>$health之外,还可以使用reat,因为它似乎显示了一个数字列表。非常感谢