Jump to content

extract ID from url


devWhiz

Recommended Posts

how can I use preg_match to take aHjpOzsQ9YI out of www.youtube.com/watch?v=aHjpOzsQ9YI, I am still unfamiliar with regex, I've been writing php for alomost 4 years now and still don't know regex lol

 

Any help is appreciated.. Thanks

 

-CLUEL3SS

Link to comment
Share on other sites

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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.