I have an URL say: www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go
. I want to extract the string register-car
from it using php code. Please help.
问问题
2000 次
4 回答
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"
?>
于 2012-12-07T03:54:20.720 回答
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];
于 2012-12-07T03:56:05.887 回答
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']);
于 2012-12-07T03:54:04.573 回答
0
您可以使用parse_url和basename的组合,甚至是pathinfo来帮助您解决这个问题。
在 PHP < 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 >= 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
于 2012-12-07T03:54:22.640 回答