php url 爆炸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15118047/
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 08:36:54  来源:igfitidea点击:

php url explode

phparraysurlexplode

提问by user1616846

I am wanting to grab my product from my url. For example:

我想从我的网址中获取我的产品。例如:

http://www.website.com/product-category/iphone

I am wanting to grab the iphone and that is fine with my code but I have a dropdown to sort products and which clicked will change the url and add a query like:

我想抓住 iphone,这对我的代码很好,但我有一个下拉菜单来对产品进行排序,点击哪个将更改 url 并添加如下查询:

http://www.website.com/product-category/iphone?orderby=popularity
http://www.website.com/product-category/iphone?orderby=new
http://www.website.com/product-category/iphone?orderby=price
http://www.website.com/product-category/iphone?orderby=price-desc

My current code is

我目前的代码是

$r = $_SERVER['REQUEST_URI']; 
$r = explode('/', $r);
$r = array_filter($r);
$r = array_merge($r, array()); 

$endofurl = $r[1];
echo $endofurl;

How is it possible to grab the iphone section all the time.

怎么可能一直抢iphone版块。

Cheers

干杯

回答by newfurniturey

You can use PHP's parse_url()function to split the URL for you and then access the pathparameter and get the end of it:

您可以使用PHP的parse_url()函数为您拆分URL,然后访问path参数并获得它的结尾:

$r = parse_url($url);
$endofurl = substr($r['path'], strrpos($r['path'], '/'));

This will parse the URL and then take a "sub-string" of the URL starting from the last-found /in the path.

这将解析 URL,然后从/路径中最后找到的开始获取 URL 的“子字符串” 。

You can alternatively use explode('/')as you're currently doing on the path:

您也可以explode('/')像当前在路径上一样使用:

$path = explode($r['path']);
$endofurl = $path[count($path) - 1];

UPDATE(using strrchr(), pointed out by @x4rf41):
A shorter method of obtaining the end of the string, opposed to substr()+ strrpos()is to use strrchr():

更新(使用strrchr(),@x4rf41 指出):
substr()+相对的一种更短的获取字符串结尾的方法strrpos()是使用strrchr()

$endofurl = strrchr($r['path'], '/');

If you take advantage of parse_url()'s option parameters, you can also get just the pathby using PHP_URL_PATHlike $r = parse_url($url, PHP_URL_PATH);

如果您利用parse_url()的选项参数,您还可以通过使用like $r = parse_url($url, PHP_URL_PATH);来获取路径PHP_URL_PATH

Or, the shortest method:

或者,最短的方法:

$endofurl = strrchr(parse_url($url, PHP_URL_PATH), '/');

回答by Mirko Adari

If you want to retrieve the last element of the array, you can use the endfunction. The rest of your code seems to be working.

如果要检索数组的最后一个元素,可以使用end函数。您的其余代码似乎正在运行。

$endofurl = end($r);

You could also leverage parse_urland strrchrfunctions to make it more concise:

您还可以利用parse_urlstrrchr函数使其更简洁:

$endofurl = strrchr(parse_url($url, PHP_URL_PATH), '/');

回答by user1616846

Just figured it out.This now works with

刚刚想通了。现在可以使用

$r = $_SERVER['REQUEST_URI']; 
$r = explode('/', $r);
$r = array_filter($r);
$r = array_merge($r, array()); 
$r = preg_replace('/\?.*/', '', $r);

$endofurl = $r[1];
echo $endofurl;