【问题标题】:php - get name of current page from url and reformat stringphp - 从 url 和格式字符串获取当前页面的名称
【发布时间】:2012-09-13 19:37:15
【问题描述】:

我有以下代码 (1) 从 url 获取页面/部分名称 (2) 清理字符串,然后将其分配给变量。

我想知道是否有任何建议可以改进此代码以提高效率,可能更少 if / else 语句。

此外,任何建议我如何编码,以便它占 url 结构中的子目录的 x 数量。现在我以非常手动的方式最多检查 3 个。

我希望它可以处理任何网址,例如:www.domain.com/level1/level2/level3/level4/levelx/...

这是我当前的代码:

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

【问题讨论】:

标签: php string url url-rewriting str-replace


【解决方案1】:

您当前正在使用 for-loop,但没有使用 $i 迭代器 - 所以对我来说,您可以完全放弃循环。据我所知,您只希望文件之前的目录名称为$pageName,如果没有先前的目录,请将其设置为homepage

您可以将$_SERVER['PHP_SELF'] 传递给basename() 以获取确切的文件名,而不是检查索引,并且还可以在/ 上拆分,就像您当前正在做的那样获取“最后一个目录”。获取最后一个目录可以跳过索引直接使用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?
}

?>

如果您想要更多 breadcumbs 的感觉,您可能希望保留每个单独的目录。如果是这种情况,您可以将中间的if else 条件更新为:

} 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 {

【讨论】:

  • 我真的很喜欢这个解决方案,但它忽略了实际的页面名称,即 www.domain.com/subdir/my-page.php,页面名称将是 subdir,所以忽略了 my-page.php。我希望它是 www : subdir : my page
  • @taulant “文件名”在变量$script中,通过basename()命令获取。您可以随意使用/格式化它(例如$pageName .= ' : ' . trim(str_replace($cleanUpArray, ' ', $page));)。
【解决方案2】:

我发现这段代码很有帮助

$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;

【讨论】:

    猜你喜欢
    • 2011-07-17
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2014-01-30
    • 2011-06-17
    • 1970-01-01
    • 2018-04-09
    相关资源
    最近更新 更多