PHP 获取 URL 的 URI 部分 [英] PHP get URI parts of URL
本文介绍了PHP 获取 URL 的 URI 部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 2 个字符串并替换它们,所以我只得到 URI 部分.
I have 2 strings and substitute them, so I get just URI parts.
$base_url = 'http://localhost/project/';
$url = 'http://localhost/project/controller/action/param1/param2';
我正在检查 URI 部分.
I am checking for URI parts.
$only_segments = str_replace($base_url, '', $real_url);
基本上我这样做:{url} - {base_url} = {only_segments}
然后我可以得到片段:
$parts = explode('/', $only_segments);
print_r($parts);
问题:
我是否走在正确的道路上,还是可以通过 $_SERVER['REQUEST_URI'] 更轻松地完成?
Am I on right path or can it be done easier with $_SERVER['REQUEST_URI'] ?
注意:我不想在 URI 部分使用 project
,它是 localhost 的子文件夹.
Note: I don't want project
in URI parts, it is sub-folder of localhost.
推荐答案
查看parse_url()
.这样你就可以完成大部分任务了.您需要做的就是删除作为基本 URL 一部分的路径部分.
Look into parse_url()
. That will get you most of the way there. All you would need to do is remove the portion of the path that is part of your base URL.
print_r(parse_url('http://localhost/project/controller/action/param1/param2'));
Array
(
[scheme] => http
[host] => localhost
[path] => /project/controller/action/param1/param2
)
这篇关于PHP 获取 URL 的 URI 部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文