【问题标题】:Getting parts of a URL in PHP在 PHP 中获取部分 URL
【发布时间】:2025-12-21 15:10:10
【问题描述】:

如何使用 PHP 函数提取以下部分:

  • 领域
  • 没有文件的路径
  • 文件
  • 带有扩展名的文件
  • 没有扩展名的文件
  • 方案
  • 港口
  • 查询
  • 片段
  • (添加您认为有用的任何其他内容)

例 1 https://*.com/users/test/login.php?q=san&u=post#top

  • 域 (*.com)
  • 没有文件的路径(/users/test/)
  • 文件(login.php)
  • 文件扩展名 (.php)
  • 没有扩展名的文件(登录)
  • 方案(https:)
  • 端口(返回空字符串)
  • 查询(q=san&u=post)
  • 片段(上)

例如:2 *.com/users/test/login.php?q=san&u=post#top

  • 域 (*.com)
  • 没有文件的路径(/users/test/)
  • 文件(login.php)
  • 文件扩展名 (.php)
  • 没有扩展名的文件(登录)
  • 方案(返回空字符串)
  • 端口(返回空字符串)
  • 查询(q=san&u=post)
  • 片段(上)

例如:3 /users/test/login.php?q=san&u=post#top

  • 没有文件的路径(/users/test/)
  • 文件(login.php)
  • 文件扩展名 (.php)
  • 没有扩展名的文件(登录)
  • 查询(q=san&u=post)
  • 片段(上)
  • 对于剩余(返回空字符串)

例如:4 /users/test/login?q=san&u=post#top

  • 没有文件的路径(/users/test/)
  • 文件(登录)
  • 文件扩展名(返回空字符串)
  • 没有扩展名的文件(登录)
  • 查询(q=san&u=post)
  • 片段(上)
  • 对于剩余(返回空字符串)

例如:5 次登录?q=san&u=post#top

  • 文件(登录)
  • 文件扩展名(返回空字符串)
  • 没有扩展名的文件(登录)
  • 查询(q=san&u=post)
  • 片段(上)
  • 对于剩余(返回空字符串)

例如:6 ?q=san&u=post

  • 查询(q=san&u=post)
  • 对于剩余(返回空字符串)

我检查了 parse_url 函数,但没有返回我需要的。因为,我是 PHP 的初学者,所以对我来说很难。如果您有任何想法,请回答。

提前致谢。

【问题讨论】:

  • 你有没有尝试过?
  • login?q=san&u=post#top -> 文件(login.php) - 在哪里.php 在输入中?
  • parse_url 没有返回什么?
  • 是的。苏加塔我已经厌倦了“parse_url”功能。但它只适用于 Ex.1(我上面已经提到过)。对于剩下的情况,它没有给出正确的答案。
  • @SanKaRan - 那么你需要什么 parse_url() 没有给你?将 parse_url() 与 pathinfo() 一起使用是否会给你一切?

标签: php regex url urlparse


【解决方案1】:

PHP 提供了一个parse_url 函数。

此函数解析一个 URL 并返回一个关联数组,其中包含 存在的 URL 的任何各种组件。

这个函数不是为了验证给定的 URL,它只会破坏 它进入上面列出的部分。部分 URL 也被接受, parse_url() 尽最大努力正确解析它们。


你可以看到测试用例executed here

$urls = array(
  "https://*.com/users/test/login.php?q=san&u=post#top",
  "/users/test/login.php?q=san&u=post#top",
  "?q=san&u=post#top",
  "login.php?q=san&u=post#top",
  "/users/test/login?q=san&u=post#top",
  "login?q=san&u=post#top"
);
foreach( $urls as $x ) {
  echo $x . "\n";
  var_dump( parse_url($x) );
}

【讨论】:

  • 我检查了 parse_url 函数,但没有返回我需要的。
  • @hipotter。谢谢。但对于我的 sec 案例,它不起作用 fyn。它的返回为 ["path"]=> string(38) "*.com/users/test/login.php"。
  • @SanKaRan 你如何区分黑白website.info/page.php?querypage.php/website.info?query?它们都可以作为path 有效,但只有一个有资格作为实际的虚拟主机。
  • @hipotter。是的,我同意你的观点。但是还有其他想法可以区分这两者。
【解决方案2】:

我正在使用它来定位根和 webroot

<?php

/**
 * @brief get paths from the location where it was executed.
 */
class PathHelper {
    /**
     * @brief This function tries to determine the FileSystem root of the application. (needs to be executed in the root)
     * @return string
     */
    public static function locateRoot($file) {
        $dir = dirname($file);
        /** FIX Required for WINDOWS * */
        $dir = preg_replace('/\\\\/', '/', $dir);
        $dir = preg_replace('/\\\/', '/', $dir);
        return $dir;
    }

    /**
     * @brief This function tries to determine the WebRoot. (needs to be executed in the root)
     * @return string
     */
    public static function locateWebroot($file) {
        $docroot = $_SERVER['DOCUMENT_ROOT'];
        $dir = dirname($file);
        if ($dir == $docroot) {
            $webroot = "";
        } else {
            $webroot = substr_replace($dir, '', 0, strlen($docroot));
        }
        /** FIX Required for WINDOWS * */
        $webroot = preg_replace('/\\\\/', '/', $webroot);
        $webroot = preg_replace('/\\\/', '/', $webroot);
        return $webroot;
    }
}

我将它设置为一个常量,以便我可以在整个应用程序中使用它。

例如:

对于菜单,您可以执行以下操作:

   // the requested url
    $requestedUrl = $_SERVER['REQUEST_URI'];

    // remove the webroot from the requested url
    $requestedUrl = str_replace(WEBROOT, "", $_SERVER['REQUEST_URI']);

    // take away the / if they still exist at the beginning
    $requestedUrl = ltrim($requestedUrl, "/");

然后我得到了这个: index.php?controller=User&action=overview

这等于我的菜单项之一的网址。 您可以在最后一个 url 上使用 explode 来查找您想要的所有其他值。

编辑:使用 parse_url() 可能更好。我不习惯 PHP 中的所有功能,但如果没有任何效果,那么这至少是一个后备。

【讨论】: