在我的 php 代码中获取没有文件扩展名的文件名

getting file name without file extention in my php code

我想在这里做的是,在下面的 php 代码中,我必须手动设置文件名,我想让它成为一些自动抓取文件名但没有文件扩展名的方式

这是我想要获取文件名的部分代码

$Pages = array(
    'clothes' => 'Clothes',
    'shirt' => 'shirt',
    'this-shirt' => 'This Shirt'
);

它说 "this-shirt" 是文件名,我希望它自动设置,而不是每次创建页面时都写下来。这里还有完整的代码

<?php
$Pages = array(
    'clothes' => 'Clothes',
    'shirt' => 'shirt',
    'this-shirt' => 'This Shirt'
);

$path = $_SERVER["PHP_SELF"];
$parts = explode('/', $path);
if (count($parts) < 2) {
    echo("home");
} else {
    echo ("<a href=\"http://domain.com\">Home</a> &raquo; ");
    for ($i = 2; $i < count($parts); $i++) {
        if (!strstr($parts[$i], ".")) {
            echo("<a href=\"");
            for ($j = 0; $j <= $i; $j++) {
                echo $parts[$j] . "/";
            };
            echo("\">" . str_replace('-', ' ', $Pages[$parts[$i]]) . "</a> &raquo; ");
        } else {
            $str = $parts[$i];
            $pos = strrpos($str, ".");
            $parts[$i] = substr($str, 0, $pos);
            echo str_replace('-', ' ', $Pages[$parts[$i]]);
        }
    }
}

希望你明白了。谢谢

应该这样做:

// get this-shirt.php from the URL
$file = basename($_SERVER["PHP_SELF"]);

// pure magic
$filename = (count(explode('.', $file)) === 1 ? $file : implode('.', array_slice(explode('.', $file), 0, (count(explode('.', $file))-1))));

$Pages = array(
    'clothes' => 'Clothes',
    'shirt' => 'shirt',
    $filename => 'This Shirt' // use $filename to declare the array's key
);