从url获取itunes id

时间:2011-12-02 04:04:32

标签: php regex

从itunes app链接中检索ID的最佳方法是什么?

让我说我有这些链接:

  

http://itunes.apple.com/us/app/bring-me-sandwiches!!/id457603026?mt=8

     

http://itunes.apple.com/us/app/bring-me-sandwiches!!/id457603026

我只是想使用php preg_match获取id,457603026

5 个答案:

答案 0 :(得分:16)

没有preg_match:

$url = 'http://itunes.apple.com/us/app/bring-me-sandwiches!!/id457603026?mt=8';
echo end(explode('/id', parse_url($url, PHP_URL_PATH)));

或者,如果您愿意:

$url = 'http://itunes.apple.com/us/app/bring-me-sandwiches!!/id457603026?mt=8';
$id = explode('/id', parse_url($url, PHP_URL_PATH));
echo $id[1];

答案 1 :(得分:12)

使用explode,假设idxxx始终位于链接的末尾: -

$id = str_replace("id", "", end(explode("/", parse_url($link, PHP_URL_PATH))));

答案 2 :(得分:4)

尝试:

$url = 'http://itunes.apple.com/us/app/bring-me-sandwiches!!/id457603026';
preg_match("/id(\d+)/", $url, $match); 
echo $match[1]; //457603026

答案 3 :(得分:4)

适用于所有类型或iTunes网址的解决方案

$urls = array('http://itunes.apple.com/us/app/bring-me-sandwiches!!/id457603026',
'http://itunes.apple.com/us/album/break-of-dawn/id472335316?ign-mpt=uo%3D',
'http://itunes.apple.com/us/app/monopoly-here-now-the-world/id299110947?mt=8',
'http://itunes.apple.com/es/app/revista-/id397781759?mt=8%3Futm_so%3Dtwitter',
'http://itunes.apple.com/app/id426698291&mt=8',
'http://itunes.apple.com/us/album/respect-the-bull-single/id4899',
'http://itunes.apple.com/us/album/id6655669');

foreach($urls as $url):
preg_match("/id(\d+)/", $url, $match); 
echo $match[1];
echo  '<br/>';
endforeach;

答案 4 :(得分:1)

试试这个,而不是正则表达式解决方案:

$url = "http://itunes.apple.com/us/app/bring-me-sandwiches!!/id457603026?mt=8";
$arr = array_pop(explode("/", $url));
$id = array_pop(array_reverse(explode("?", $arr)));
echo substr($id, 2, strlen($id));