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

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 {

I found this code very helpful

$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === 
FALSE ? 'http' : 'https';            // Get protocol HTTP/HTTPS

$host     = $_SERVER['HTTP_HOST'];   // Get  www.domain.com

$script   = $_SERVER['SCRIPT_NAME']; // Get folder/file.php

$params   = $_SERVER['QUERY_STRING'];// Get Parameters occupation=odesk&name=ashik

$currentUrl = $protocol . '://' . $host . $script . '?' . $params; // Adding all

echo $currentUrl;