获取排除查询字符串的最后一个URI段

时间:2012-12-07 03:48:02

标签: php

我有一个网址:www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go。我想使用php代码从中提取字符串register-car。请帮忙。

4 个答案:

答案 0 :(得分:2)

试试这个:

<?php 

 $url = "www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go";

 $register_car = basename(parse_url($url, PHP_URL_PATH));

 echo $register_car; // will echo "register-car"

?>

答案 1 :(得分:0)

您正在寻找parse_url函数,后跟basename返回最后一个/ item /。以下打印register-car

  $parsed = parse_url("http://www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go", PHP_URL_HOST);
  echo basename($parsed['path']);

答案 2 :(得分:0)

您可以结合使用parse_urlbasename,甚至pathinfo来帮助您解决此问题。

在PHP&lt; 5.4.7

$url = "www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go";
$result = basename(parse_url("http://" . $url, PHP_URL_PATH));
echo $result; // register-car

在PHP中&gt; = 5.4.7

$url = "www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go";
$result = basename(parse_url("//" . $url, PHP_URL_PATH));
echo $result; // register-car

答案 3 :(得分:0)

$parts = parse_url("www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go", PHP_URL_PATH);
$pieces = explode($parts, "/");
$action = $pieces[count($pieces)-1];