使用 php 将 URL 拆分为页面和文件夹变量?
我目前正在使用以下代码从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用更通用的拆分怎么样?
然后根据需要进行路由:
What about using more general splitting ?
Then do your routing as you want:
那么您想要如下的 URI 方案吗?
特殊情况:
您应该使用
explode()
或preg_split()
将字符串拆分为数组,而不是依赖basename()
。您可以直接使用REQUEST_URI
,因为域名不会为您的部分和页面提供任何额外信息。一旦有了数组,您就可以轻松地
count()
路径组件的数量,处理空路径和大小为 1 的路径的特殊情况,等等。在下面的示例中,我使用 array_pop() 提取路径的最后一部分,以将页面与部分分开。由于您似乎希望部分和页面使用空格分隔的字符串,因此我使用implode()
将数组重新连接到字符串中。So you want an URI-scheme as follows?
Special cases:
Instead of relying on
basename()
, you should split your string into an array usingexplode()
orpreg_split()
. You can useREQUEST_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 usearray_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 useimplode()
to join the arrays back into a string.