【发布时间】:2017-02-14 14:48:58
【问题描述】:
我在这里尝试做什么,在下面的 php 代码中,我必须手动设置文件名,我想让它自动获取文件名但没有文件扩展名
这是我想要获取文件名的部分代码
$Pages = array(
'clothes' => 'Clothes',
'shirt' => '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> » ");
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> » ");
} else {
$str = $parts[$i];
$pos = strrpos($str, ".");
$parts[$i] = substr($str, 0, $pos);
echo str_replace('-', ' ', $Pages[$parts[$i]]);
}
}
}
希望你能明白。谢谢
【问题讨论】:
-
$file = 'this-shirt.pdf'; $filename = (count(explode('.', $file)) === 1 ? $file : implode('.', array_slice(explode('.', $file), 0, (count(explode('.', $file))-1)))); echo $filename; -
@MonkeyZeus 亲爱的你能把你的代码贴在答案上吗?因为像这样不清楚
-
哪个变量包含
this-shirt和扩展名? -
this-shirt 是类似 this-shirt.php 的文件名,但每次我将整个代码放在我创建的页面上时,我都必须将其更改为当前页面名称。而不是所有我想要的 php 在那里自己抓取文件名。我不想创建例如名为 my-file.php 的文件并将整个代码放在该页面上并将这件衬衫更改为 my-file
-
我明白了。请在下面查看我的答案。