使用PHP分割URL [英] Using PHP to split a URL

查看:274
本文介绍了使用PHP分割URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个PHP代理,其中它接受一个URL并确认它在我的服务器列表中.

I am creating a PHP proxy where it accepts a url and confirms it is on my list of servers.

从应用程序导入URL时,我遇到了需要2个解析器标签的问题.我需要它沿着"\"分开标签以及字符串(在我的情况下为导出?"

When importing the url from the application i ran it to an issue where i needed 2 parser tags. i need it to split along a "\?" tag as well as a string, in my case, "export?"

我正在使用preg作为第一个标签.这是否接受像我的导出标签这样的字符串,或者是否有其他方法可以做到这一点?

i am using preg for the first tag. Does this accept the strings like my export tag or is there some other method for doing this?

请让我知道这是如何完成的,或者您还有其他问题.

please le me know how this is accomplished or if you have more questions.

推荐答案

parse_url .

As ircmaxell has already stated in the comments, PHP does already have a function to parse a URL: parse_url.

并且当您具有URL路径(假设您的export?路径后缀加上查询指示符)时,可以使用 explode 将路径分为其路径段:

And when you have the URL path (I assume your export? the path suffix plus the query indicator), you can use explode to split the path into its path segments:

$path = parse_url($url, PHP_URL_PATH);
$segments = explode('/', $path);

然后您可以使用以下其中一项获取最后的路径段:

You can then get the last path segment with one of the following:

end($segments)
$segments[count($segments)-1]

要处理结尾的斜杠,可以使用rtrim($path, '/')删除它们.

And to cope with trailing slashes, you can use rtrim($path, '/') to remove them.

一起:

$url = 'http://www.example.com/subfolders/export?';
$path = parse_url($url, PHP_URL_PATH);
$segments = explode('/', rtrim($path, '/'));
echo end($segments);

这篇关于使用PHP分割URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆