How to get video id from embed URL using PHP? -


this question has answer here:

i having youtube embed url this

<object width="420" height="315"> <param name="movie" value="//www.youtube.com/v/whtwjg4zijg?version=3&hl=en_us"></param> <param name="allowfullscreen" value="true"></param><param name="allowscriptaccess" value="always"></param> <embed src="//www.youtube.com/v/whtwjg4zijg?version=3&hl=en_us" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true"> </embed></object> 

now want id of video using php. please help

you use id of youtube link:

$your_url='http://www.youtube.com/watch?var1=blabla#v=gvjehzx3eq1$var2=bla';  function get_youtube_id_from_url($url){     if (stristr($url,'youtu.be/'))         { preg_match('/(https:|http:|)(\/\/www\.|\/\/|)(.*?)\/(.{11})/i', $url, $final_id); return $final_id[4]; }     else          { preg_match('/(https:|http:|):(\/\/www\.|\/\/|)(.*?)\/(embed\/|watch\?v=|(.*?)&v=|v\/|e\/|.+\/|watch.*v=|)([a-z_a-z0-9]{11})/i', $url, $idd); return $idd[6]; } }  echo get_youtube_id_from_url($your_url); 

output:

gvjehzx3eq1 

and youtube link embed tags:

$slink = "<embed src=\"www.youtube.com/v/whtwjg4zijg?version=3&hl=en_us\"></embed>"; preg_match('/< *embed[^>]*src *= *["\']?([^"\']*)/i', $slink,$amatch); echo $amatch[1]; 

output:

www.youtube.com/v/whtwjg4zijg?version=3&hl=en_us 

you have use little bit of logic make both these functions work :) starting point!

bear in mind " need escaped \ if enclosing expression in double quotes.

p.s: got first function somewhere, don't remember where.

edit

you use javascript this:

var re = /(\?v=|\/\d\/|\/embed\/|\/v\/|\.be\/)([a-za-z0-9\-\_]+)/; var urlarr = [     "http://www.youtube.com/watch?v=0zm3napsvmg&feature=feedrec_grec_index",     "http://www.youtube.com/user/ingridmichaelsonvevo#p/a/u/1/qdk8u-vih_o",               ]; (var = 0; < urlarr.length; i++) {     alert(urlarr[i].match(re)[2]); } 

jsfiddle: http://jsfiddle.net/a7lyz/


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -