Jump to content

extract ID from url


devWhiz

Recommended Posts

Hi,

Try this code:

$url = 'www.youtube.com/watch?v=aHjpOzsQ9YI';
preg_match('/v=([a-z0-9]+)$/i', $url, $mc);
echo $mc[1];      // aHjpOzsQ9YI

 

That won't always work, youtube uses other characters as well.

 

as requinix said do this:

$url = "www.youtube.com/watch?v=aHjpOzsQ9YI";
$pu = parse_url($url);
// php 5.4: $query = parse_url($url)["query"];
// other:
$query = $pu["query"];
$parameters = parse_str($query);
print_r($parameters);

Link to comment
https://forums.phpfreaks.com/topic/263890-extract-id-from-url/#findComment-1352397
Share on other sites

  • 2 weeks later...

TLG parse_url() accepts an optional 2nd argument to only return the portion you want.  Also, parse_str() returns void.  You have to specify the array as the 2nd argument.

 


url = "www.youtube.com/watch?v=aHjpOzsQ9YI";
$query = parse_url($url,PHP_URL_QUERY);
parse_str($query,$params);
echo $params['v'];

Link to comment
https://forums.phpfreaks.com/topic/263890-extract-id-from-url/#findComment-1356041
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.