PHP的爆炸网址 [英] php url explode

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

问题描述

我想从我的网址抢我的产品。例如:

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

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

我想抢到iphone,这是罚款与我的code,但我有一个下拉列表来排序的产品和点击将改变网址,并添加一个查询,如:

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:

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

我目前的code是

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;

这怎么可能抢iphone节所有的时间。

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

干杯

推荐答案

您可以使用PHP的 parse_url( ) 功能分割的网址给你,然后访问路径参数,并得到它的结束:

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

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

这将解析URL,然后采取URL的串子,从开始的最后一个发现的 / 路径。

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

您可以选择使用爆炸('/')作为目前工作的道路上:

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

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

更新(使用 strrchr() ,指出了@ x4rf41):结果
获得字符串的结尾,而不是 SUBSTR() + strrpos()是使用更短的方法 strrchr()

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():

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

如果您利用 parse_url的()的选项参数,你还可以得到的只是路径的使用 PHP_URL_PATH
$ R = parse_url($网址,PHP_URL_PATH);

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

或者,最短的方法:

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

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

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