获取文件名没有文件扩展名在我的PHP代码
问题描述:
我想在这里做什么,在我的PHP代码下面我必须手动设置文件名,我想使它一些如何抓住文件名自动但没有文件扩展获取文件名没有文件扩展名在我的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]]);
}
}
}
希望你明白了。感谢
答
这应做到:
// 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
);
+0
工作兄弟。完全像我想要的一样。非常感谢你 –
+0
亲爱的你可以用这个帮助我吗http://stackoverflow.com/questions/42262290/need-help-in-my-php-code-getting-wrong-directory-on-breadcrumb –
'$文件= '这-shirt.pdf'; $ filename =(count(explode('。',$ file))=== 1?$ file:implode('。',array_slice(explode('。',$ file),0,(count(explode(' 。',$ file)) - 1)))); echo $ filename;' – MonkeyZeus
@MonkeyZeus亲爱的你可以请发表你的代码在答案?因为它不是这样清楚的 –
哪个变量包含带有**扩展名的“this-shirt **”? – MonkeyZeus