如何从PHP中获取URL中的特定值

时间:2016-04-22 12:07:08

标签: php

我有一个网址:

market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001

如何从以上网址000146132647632302db63d958690001

中获取此值

我可以使用preg_match功能还是其他功能。

3 个答案:

答案 0 :(得分:3)

如果您将其作为真实网址接收而不是简单:

echo $_GET['trackingid'];

否则:

$queryArray = [];
$query = parse_url(
    urldecode("market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001"),
    PHP_URL_QUERY
);
parse_str($query, $queryArray);
echo $queryArray['trackingid'];

Live example

答案 1 :(得分:1)

您可以使用正则表达式。否则你可以使用$ _GET

直接访问它
$url='market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001';

if(preg_match("/([^\?]*)\?trackingid%(d*)/",$url,$matches)){
  echo $matches[1];
} else {
  $_GET['trackingid']
}

答案 2 :(得分:0)

1)一种方法是使用explode()

$test = "market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001";
    $url = explode("trackingid=",urldecode($test));
    echo $url[1];

工作演示:Click Here

2)另一个是你可以使用preg_match()来实现它。

3)如果您使用网址获取,请使用 $_GET['trackingid'] 获取。

4)使用parse_str()

$url = urldecode("market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001");
parse_str($url, $tempArray);
echo $tempArray['trackingid'];