Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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_Urldecode - Fatal编程技术网

在php中输出URL的一部分

在php中输出URL的一部分,php,urldecode,Php,Urldecode,因此,我动态生成了以下格式的页面: http://example.com/name/first_name/last_name/John%2C+Smith 输出url最后一部分的php代码是什么 所以,它变成了“约翰,史密斯” 非常感谢你 编辑: 我意识到该URL以另一个/结尾,下面给出的答案并不符合要求。我应该做什么改变 http://example.com/name/first_name/last_name/John%2C+Smith/ 编辑2: 因此,链接是动态生成的,如下所示: hr

因此,我动态生成了以下格式的页面:

 http://example.com/name/first_name/last_name/John%2C+Smith
输出url最后一部分的php代码是什么

所以,它变成了“约翰,史密斯”

非常感谢你

编辑:

我意识到该URL以另一个
/
结尾,下面给出的答案并不符合要求。我应该做什么改变

http://example.com/name/first_name/last_name/John%2C+Smith/
编辑2:

因此,链接是动态生成的,如下所示:

href="http://example.com/name/first_name/last_name/<?php echo $full_name ?>"
href=”http://example.com/name/first_name/last_name/"

拆分url,获取最后一个片段,然后url解码:

<?
$urlarray=explode("/",$url);
$end=$urlarray[count($urlarray)-1];
$end=urldecode($end);
//go on using $end then
?>

您可以使用正则表达式执行此操作

echo preg_replace_callback('~.*/(.*)~', 
     function($matches) { 
          return urldecode($matches[1]);
     },
     'http://example.com/name/first_name/last_name/John%2C+Smith');
正则表达式演示:

输出:

约翰,史密斯

更新:

echo preg_replace_callback('~.*/(.+)~', 
function($matches) { 
     return rtrim(urldecode($matches[1]), '/');
},
'http://example.com/name/first_name/last_name/John%2C+Smith/');

您可以将
parse\u url
与第二个参数
PHP\u url\u PATH

$url = urldecode("http://example.com/name/first_name/last_name/John%2C+Smith");
$arr = array_filter(explode('/',parse_url($url, PHP_URL_PATH)));
print_r(end($arr));
编辑:

根据动态url的要求,您可以使用

$url = urldecode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");

我想这已经在这里得到了回答:[你也可以运行
array\u reverse
,简化一个步骤,
$urlarray=array\u reverse(explode(“/”,$url));echo urldecode($urlarray[0]);
。刚刚注意到url以另一个
/
(“/John%2C+Smith/”)结尾。我应该做些什么更改才能让它工作?(我更新了问题)@steveKim
$url=trim($url,“/”)
explode之前explode根据返回数组。你真的不应该调用数组-$str。这是一个简单的变量,我已经用过了,为了更方便起见,我将它更新为$arr@geneI。我意识到最后有一个
/
,我想知道应该做什么更改才能使代码正常工作。谢谢。检查我的更新答案。我放置了
array\u filter
函数。这个答案非常有效。我不知道为什么会有反对票。我意识到最后有一个
/
,我想知道我应该做些什么更改才能使代码正常工作。谢谢。写的更新应该解释
//code>>或者最后没有
/
。我有一个问题。url是动态生成的,因此最后一部分总是不同的,例如
http://example.com/name/first_name/last_name/Mike%2C+泰森/
。在这种情况下,上述答案有效吗?(我编辑了我的问题,请参见
编辑2
)是的,不管有没有尾斜杠,这都可以。试试看。