duanchai0028 2017-02-14 14:48
浏览 43
已采纳

在我的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'
);

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

  • 写回答

1条回答 默认 最新

  • duanlei0282 2017-02-14 15:47
    关注

    This should do it:

    // 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
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 《小魔指》街机游戏机整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?