duanduan8439 2012-09-13 19:37
浏览 89
已采纳

php - 从url获取当前页面的名称并格式化字符串

I have the following code that (1) gets the page / section name from the url (2) cleans up the string and then assigns it to a variable.

I was wondering if there are any suggestions to how I can improve this code to be more efficient, possibly less if / else statements.

Also, any suggestion how I can code this so that it accounts for x amount of sub-directories in the url structure. Right now I check up to 3 in a pretty manual way.

I'd like it to handle any url, for example: www.domain.com/level1/level2/level3/level4/levelx/...

Here is my current code:

<?php

    $prefixName = 'www : ';
    $getPageName = explode("/", $_SERVER['PHP_SELF']);
    $cleanUpArray = array("-", ".php");

    for($i = 0; $i < sizeof($getPageName); $i++) {
        if ($getPageName[1] == 'index.php')
        {
            $pageName = $prefixName . 'homepage';
        }
        else
        {
            if ($getPageName[1] != 'index.php')
            {                   
                $pageName = $prefixName . trim(str_replace($cleanUpArray, ' ', $getPageName[1]));
            } 
            if (isset($getPageName[2]))
            {
                if ( $getPageName[2] == 'index.php' )
                {
                    $pageName = $prefixName . trim(str_replace($cleanUpArray, ' ', $getPageName[1]));
                }               
                else
                {
                    $pageName = $prefixName . trim(str_replace($cleanUpArray, ' ', $getPageName[2]));
                }
            }
            if (isset($getPageName[3]) )
            { 
                if ( $getPageName[3] == 'index.php' )
                {
                    $pageName = $prefixName . trim(str_replace($cleanUpArray, ' ', $getPageName[2]));
                }               
                else
                {
                    $pageName = $prefixName . trim(str_replace($cleanUpArray, ' ', $getPageName[3]));
                }
            }   
        }           
    }
?>
  • 写回答

2条回答 默认 最新

  • douwa6220 2012-09-13 20:01
    关注

    You are currently using a for-loop, but not using the $i iterator for anything - so to me, you could drop the loop entirely. From what I can see, you just want the directory-name prior to the file to be the $pageName and if there is no prior directory set it as homepage.

    You can pass $_SERVER['PHP_SELF'] to basename() to get the exact file-name instead of checking the indexes, and also split on the / as you're currently doing to get the "last directory". To get the last directory, you can skip indexes and directly use array_pop().

    <?php
    
    $prefixName = 'www : ';
    $cleanUpArray = array("-", ".php");
    
    $script = basename($_SERVER['PHP_SELF']);
    $exploded = explode('/', substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/')));
    $count = count($exploded);
    
    if (($count == 1) && ($script == 'index.php')) {
        // the current page is "/index.php"
        $pageName = $prefixName . 'homepage';
    } else if ($count > 1) {
        // we are in a sub-directory; use the last directory as the current page
        $pageName = $prefixName . trim(str_replace($cleanUpArray, ' ', array_pop($exploded)));
    } else {
        // there is no sub-directory and the script is not index.php?
    }
    
    ?>
    

    In the event that you want a more breadcumbs-feel, you may want to keep each individual directory. If this is the case, you can update the middle if else condition to be:

    } else if ($count > 1) {
        // we are in a sub-directory; "breadcrumb" them all together
        $pageName = '';
        $separator = ' : ';
        foreach ($exploded as $page) {
            if ($page == '') continue;
            $pageName .= (($pageName != '') ? $separator : '') . trim(str_replace($cleanUpArray, ' ', $page));
        }
        $pageName = $prefixName . $pageName;
    } else {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R