John Doe John Doe - 3 months ago 7
PHP Question

Exploding a url in PHP

How can I obtain just the ID portion of this url.

281805943&buying=true&liking=false&download=true&sharing=false&show_artwork=false&hide_related=true&show_comments=false&show_playcount=false&show_user=false&color=e55b5b&color_theme=f5f5f5&auto_play=


I currently have exploded the url which contained the API path + the actual link but trying to obtain the ID
281805943
has proven to be a little harder, please note that we can't just echo out
281805943
because I am scraping a website and gathering their urls, this id will be different every time how ever the format will remain the same.

Original URL format looks like the following.

https://w.soundcloud.com/player/?url=https://api.soundcloud.com/tracks/281805943&buying=true&liking=false&download=true&sharing=false&show_artwork=false&hide_related=true&show_comments=false&show_playcount=false&show_user=false&color=e55b5b&color_theme=f5f5f5&auto_play=


I've currently exploded it using this

$d2 = $dl->src;
$test = explode("/", $d2);
$test[8];

Answer

If the format is always exactly the same then you can do the below using the $test[8] variable you already have:

parse_str($test[8], $output);
$id = key($output);

Or a single liner:

$id = explode('&', $test[8])[0];

There are many ways to skin a cat