0

Below is a line from an XML feed ( YouTube API 2.1 ):

<media:player url='http://www.youtube.com/watch?v=ksrHwD9cZjQ&amp;feature=youtube_gdata_player'/>

How can I use PHP ( SimpleXml ) to retrieve only the video ID which is in our case ksrHwD9cZjQ located between ?v= and &amp; ?

I used $url = (string)$media->group->player->attributes()->url; but it retrieves the entire URL.

Regards,

Thank you !

Ismail
  • 725
  • 8
  • 23

1 Answers1

1

You can further process the url by using parse_str to get the video ID in the url:

$query_string = parse_url($url, PHP_URL_QUERY); // decode `&amp;` to `&`
parse_str($query_string, $data);
echo $data['v']; // ksrHwD9cZjQ
hakre
  • 193,403
  • 52
  • 435
  • 836
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Thanks for the code, and the edit to decode, it wouldn't work without it. Appreciated, have a nice day ! – Ismail Apr 17 '15 at 14:22