0

I need to get the item ID from the iTunes url and it used to work like this before itunes changed there url structure;

$musicid = 'https://itunes.apple.com/album/the-endless-bridge-single/id1146508518?uo=1&v0=9989';
$musicid=explode('/id',$musicid);
$musicid=explode('?',$musicid[1]);
echo $musicid[0];

But now iTunes has deleted the 'id' prefix in the url so the above code does not return the id anymore, does anyone know a solution?

old itunes url; https://itunes.apple.com/album/the-endless-bridge-single/id1146508518?uo=1&v0=9989

new itunes url; https://itunes.apple.com/album/the-endless-bridge-single/1146508518?uo=1&v0=9989

jaakos
  • 5
  • 2

3 Answers3

1

You would just explode on the fourth slash, grabbing the fifth segment.

Simply remove id from /id (so that you explode() on /), and check the sixth index instead of the second (with [5] instead of [1]):

<?php

$musicid = 'https://itunes.apple.com/album/the-endless-bridge-single/1146508518?uo=1&v0=9989';
$musicid = explode('/', $musicid);
$musicid = explode('?', $musicid[5]);
echo $musicid[0]; // 1146508518

This can be seen working here.

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
1

Use a regular expression:

<?php
$url = 'https://itunes.apple.com/album/the-endless-bridge-single/1146508518?uo=1&v0=9989';

preg_match('/album\/[^\/]+\/(\d+)\?/', $url, $matches);

var_dump($matches[1]);

Demo here: https://3v4l.org/tF9pS

Cookie Guru
  • 397
  • 2
  • 10
0

Looks like you can simply use a combination of parse_url and basename, eg

$musicid = basename(parse_url($musicid, PHP_URL_PATH));

Demo ~ https://eval.in/895029

Phil
  • 157,677
  • 23
  • 242
  • 245