John Doe John Doe - 1 year ago 65
PHP Question

Exploding a url in PHP

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


I currently have exploded the url which contained the API path + the actual link but trying to obtain the ID
has proven to be a little harder, please note that we can't just echo out
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.

I've currently exploded it using this

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download