使用 php 将 URL 拆分为页面和文件夹变量?

发布于 2024-11-09 21:05:43 字数 1406 浏览 1 评论 0原文

我目前正在使用以下代码从 url 实现页面、部分和类变量。

$domain = 'http://' . $_SERVER['HTTP_HOST'];
$path = $_SERVER['REQUEST_URI'];
$url = $domain . $path;

// page + section + class
$page = basename($url);
$page = $class = str_replace('.php','',$page);
$page = str_replace('-',' ',$page);

if ($path == "/") {
    $section = $class = "home";
} else if (basename(dirname($url),"/") == $_SERVER['HTTP_HOST']) {
    $section = $page;
} else { 
    $section = basename(dirname($url),"/");
    $section = str_replace('-',' ',$section);
    $class =  basename(dirname($url),"/") . " " . $class;
}

例如,如果 url 为 http://www.mydomain.co.uk/about/,则代码将返回以下变量:

$page = "about"
$section = "about"
$class = "about"

对于 http://www.mydomain.co.uk /about/general-info/

$page = "general info"
$section = "about"
$class = "about general-info"

但是当我添加更多深度时,例如 For http://www.mydomain.co.uk/about/general-info/history/ 代码会产生:

$page = "history"
$section = "general info"
$class = "general-info history"

理想情况下,我需要它输出以下内容:

$page = "history"
$section = "about general info"
$class = "about general-info history"

或根据需要将各个部分分解为多个部分,例如:

$section1 = "about"
$section2 = "general-info"

希望有人可以提供帮助。如果有任何不清楚的地方请询问。

I am currently using the following code to achieve a page, section and class variable from the url.

$domain = 'http://' . $_SERVER['HTTP_HOST'];
$path = $_SERVER['REQUEST_URI'];
$url = $domain . $path;

// page + section + class
$page = basename($url);
$page = $class = str_replace('.php','',$page);
$page = str_replace('-',' ',$page);

if ($path == "/") {
    $section = $class = "home";
} else if (basename(dirname($url),"/") == $_SERVER['HTTP_HOST']) {
    $section = $page;
} else { 
    $section = basename(dirname($url),"/");
    $section = str_replace('-',' ',$section);
    $class =  basename(dirname($url),"/") . " " . $class;
}

For example if the url is http://www.mydomain.co.uk/about/ the code will return the following variables:

$page = "about"
$section = "about"
$class = "about"

For http://www.mydomain.co.uk/about/general-info/

$page = "general info"
$section = "about"
$class = "about general-info"

But when I add more depth for example For http://www.mydomain.co.uk/about/general-info/history/ to code produces:

$page = "history"
$section = "general info"
$class = "general-info history"

where ideally I need it to output the following:

$page = "history"
$section = "about general info"
$class = "about general-info history"

or breakdown the sections into as many as needed for example:

$section1 = "about"
$section2 = "general-info"

Hopefully someone can help. If anything is unclear please ask.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼亦尘 2024-11-16 21:05:43

使用更通用的拆分怎么样?

// Url: http://www.mydomain.co.uk/about/general-info/history/
$slices = explode('/', $_SERVER['REQUEST_URI']);
// $slices == ['about', 'general-info', 'history']

然后根据需要进行路由:

$class = implode(' ', $slices);
$section = $slices[1];
// etc.

What about using more general splitting ?

// Url: http://www.mydomain.co.uk/about/general-info/history/
$slices = explode('/', $_SERVER['REQUEST_URI']);
// $slices == ['about', 'general-info', 'history']

Then do your routing as you want:

$class = implode(' ', $slices);
$section = $slices[1];
// etc.
以可爱出名 2024-11-16 21:05:43

那么您想要如下的 URI 方案吗?

http://<domain>/<section-1>/<section-2>/.../<page>/

特殊情况:

(1) http://<domain>/            -- page is empty, section is "home"
(2) http://<domain>/<section>/  -- page and section are '<section>'

您应该使用 explode()preg_split() 将字符串拆分为数组,而不是依赖 basename()。您可以直接使用 REQUEST_URI,因为域名不会为您的部分和页面提供任何额外信息。

一旦有了数组,您就可以轻松地 count() 路径组件的数量,处理空路径和大小为 1 的路径的特殊情况,等等。在下面的示例中,我使用 array_pop() 提取路径的最后一部分,以将页面与部分分开。由于您似乎希望部分和页面使用空格分隔的字符串,因此我使用 implode() 将数组重新连接到字符串中。

// No need for the domain stuff!
$path = $_SERVER['REQUEST_URI'];

// Split at '/', could use explode() but the PREG_SPLIT_NO_EMPTY flag is
// very handy since it handles "//" and "/" at start/end.
$tokens = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);

if (count($tokens) == 0) {
    // Special case 1
    $page = "";
    $section = $class = 'home';
} elseif (count($tokens) == 1) {
    // Specical case 2
    $page = $section = $class = $tokens[0];
} else {
    // Class contains all tokens.
    $class = implode(' ', $tokens);

    // The last part is the page.
    $page = array_pop($tokens);

    // Everything else are sections.
    $sections = implode(' ', $tokens);
}

// You seem to want spaces for dashes in the section:
$section = str_replace('-', ' ', $section);

So you want an URI-scheme as follows?

http://<domain>/<section-1>/<section-2>/.../<page>/

Special cases:

(1) http://<domain>/            -- page is empty, section is "home"
(2) http://<domain>/<section>/  -- page and section are '<section>'

Instead of relying on basename(), you should split your string into an array using explode() or preg_split(). You can use REQUEST_URI directly since the domain name does not give any extra information for your sections and page.

Once you have an array, you can easily count() the number of path components, handle special cases for empty and size-one paths, and so on. In the following example, I use array_pop() to extract the last part of the path to separate the page from the sections. Since you seem to desire space-separated strings for sections and page, I use implode() to join the arrays back into a string.

// No need for the domain stuff!
$path = $_SERVER['REQUEST_URI'];

// Split at '/', could use explode() but the PREG_SPLIT_NO_EMPTY flag is
// very handy since it handles "//" and "/" at start/end.
$tokens = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);

if (count($tokens) == 0) {
    // Special case 1
    $page = "";
    $section = $class = 'home';
} elseif (count($tokens) == 1) {
    // Specical case 2
    $page = $section = $class = $tokens[0];
} else {
    // Class contains all tokens.
    $class = implode(' ', $tokens);

    // The last part is the page.
    $page = array_pop($tokens);

    // Everything else are sections.
    $sections = implode(' ', $tokens);
}

// You seem to want spaces for dashes in the section:
$section = str_replace('-', ' ', $section);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文