Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 去掉url的最后一部分_Php_Url_Preg Replace_Parse Url - Fatal编程技术网

Php 去掉url的最后一部分

Php 去掉url的最后一部分,php,url,preg-replace,parse-url,Php,Url,Preg Replace,Parse Url,可能重复: 我有这样一个url: http://www.domain.co.uk/product/SportingGoods/Cookware/1/B000YEU9NA/Coleman-Family-Cookset 我只想从“科尔曼家庭厨具”结尾提取产品名称 当我使用parse_url和print_r时,我得到以下结果: $tobestripped=$<the array name>['path']; //<<-the entire path that is $exp

可能重复:

我有这样一个url:

http://www.domain.co.uk/product/SportingGoods/Cookware/1/B000YEU9NA/Coleman-Family-Cookset
我只想从“科尔曼家庭厨具”结尾提取产品名称

当我使用parse_url和print_r时,我得到以下结果:

$tobestripped=$<the array name>['path']; //<<-the entire path that is
$exploded=explode("/", $tobestripped);
$lastpart=array_pop($exploded);
数组(
[方案]=>http
[主机]=>www.domain.co.uk
[路径]=>/product/SportingGoods/Cookware/1/B000YEU9NA/Coleman家庭厨具
) 
然后我如何修剪“科尔曼家庭厨具”的结尾

提前谢谢

$url = 'http://www.domain.co.uk/product/SportingGoods/Cookware/1/B000YEU9NA/Coleman-Family-Cookset';
$url = explode('/', $url);
$last = array_pop($url);
echo $last;

您拥有path变量(来自如上所示的数组)。 使用以下命令:

$tobestripped=$<the array name>['path']; //<<-the entire path that is
$exploded=explode("/", $tobestripped);
$lastpart=array_pop($exploded);

$tobestripped=$['path']// 以上所有答案都有效,但都使用了不必要的数组和正则表达式,您需要一个last
/
的位置,您可以使用该位置来获取字符串,而不必使用以下位置来提取字符串:

您可能需要在
strrpos()之后添加
+/-1

这是一个比使用
preg.*
explode
更有效的解决方案,但所有这些都可以工作。

好的,我是这样做的:'$lastPart=parse_url($url)$lastPart=$lastPart[path]$lastPart=分解(“/”,$lastPart);echo$lastPart[6];“”有什么改进吗?
substr( $url, 0, strrpos( $url, '/'));