2

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];
1
  • 1
    is the ID always going after tracks section? Commented Sep 8, 2016 at 16:50

4 Answers 4

2

Sounds like a job for parse_url, from the PHP docs....

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
$blob = parse_url($url, PHP_URL_PATH);
echo $blob['query'];  //arg=value
echo $blob['anchor'];  //anchor
Sign up to request clarification or add additional context in comments.

Comments

2

Not exactly pretty but it does find the id

$url="https://w.soundcloud.com/player/?url=https://api.soundcloud.com/tracks/281805943&amp;buying=true&amp;liking=false&amp;download=true&amp;sharing=false&amp;show_artwork=false&amp;hide_related=true&amp;show_comments=false&amp;show_playcount=false&amp;show_user=false&amp;color=e55b5b&amp;color_theme=f5f5f5&amp;auto_play=";
preg_match('@/tracks/(\d+)&@',$url,$matches);
echo 'id='.$matches[1];

Comments

2

Assuming your ID is always between 'tracks/' and '&amp':

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

$pattern = "/(tracks\/)([\d]+)(\&amp)/";
preg_match($pattern,$url,$matches,PREG_OFFSET_CAPTURE);

var_dump($matches[2][0]);

Comments

1

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

1 Comment

Appreciate that help my friend, nice sense of humor also. I too enjoy skinning cats.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.