PHP,如何检索特定数字?

时间:2012-11-14 01:41:41

标签: php

我需要从URL地址检索itunes app id。

https://itunes.apple.com/us/app/angry-birds-star-wars/id557137623?mt=8

我使用了strpos(),但这个没用。

如何从此地址获取身份后的号码?

$ url ='https://itunes.apple.com/us/app/angry-birds-star-wars/id557137623?mt=8';

$ result ='557137623'

结果就像这个。

2 个答案:

答案 0 :(得分:3)

$url = 'https://itunes.apple.com/us/app/angry-birds-star-wars/id557137623?mt=8';
preg_match("~id(\d+)~", $url, $matches);
$result = $matches[1];
echo $result; //557137623

答案 1 :(得分:1)

// Find where the / is and add 2 for the word "id"
$pos = strrpos($url, "/") + 2; // Note that it's "strrpos" not "strpos"

// Find the question mark at the end
$pos2 = strpos($url, "?");

$id = substr($url, $pos, $pos2 - $pos);

当然,这是假设您的网址始终包含此表单。