php php如何分割url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5257243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:46:20  来源:igfitidea点击:

php how to split url

phpstringurl

提问by pamella

how to split this url ?

如何拆分这个网址?

http://www.example.com/news?q=string&f=true&id=1233&sort=true

http://www.example.com/news?q=string&f=true&id=1233&sort=true

i need exampleonly

我只需要例子

回答by Select0r

use parse_url
http://php.net/manual/en/function.parse-url.php

使用parse_url
http://php.net/manual/en/function.parse-url.php

it returns an array with the components of the given URL

它返回一个包含给定 URL 组件的数组

you can use parse_strto further split the query string
http://php.net/manual/en/function.parse-str.php

您可以使用parse_str进一步拆分查询字符串
http://php.net/manual/en/function.parse-str.php

回答by Andrew

Use parse_url like below:

使用 parse_url 如下所示:

<?php
$url = 'http://www.example.com/news?q=string&f=true&id=1233&sort=true';

$values = parse_url($url);

$host = explode('.',$values['host']);

echo $host[1];

?>

This would work for any url that has the sub domain included (www. etc)

这适用于任何包含子域的 url(www. 等)

The PHP documentation can be found here: http://php.net/manual/en/function.parse-url.php

PHP 文档可以在这里找到:http: //php.net/manual/en/function.parse-url.php