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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程