在我的php代码中获取没有文件扩展名的文件名 [英] getting file name without file extention in my php code

查看:124
本文介绍了在我的php代码中获取没有文件扩展名的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在这里做什么,在下面的php代码中,我必须手动设置文件名,并且要使其自动获取文件名而不进行文件扩展

what i'm trying to do here, in my php code below i have to set file name manually and i want to make it some how it grabds the file name automatically but without file extention

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

here's part of my code where i want to get file name

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

其中说"this-shirt"是文件名,我希望它被自动设置,而不是每次创建页面时都将其写下来.这也是完整的代码

where it says "this-shirt" is file name and i want it to be set automatically instead of i write it down everytime i create a page. also here's full code

<?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]]);
        }
    }
}

希望您有这个主意.谢谢

hope you get the idea. thanks

推荐答案

这应该做到:

// 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
);

这篇关于在我的php代码中获取没有文件扩展名的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆