Posted By

KabuqueJoe on 04/20/11


Tagged

regex url php link video id pregreplace YouTube


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

tribal
elavender


youtube url video id


 / Published in: PHP
 

Returns the video id for any type of youtube url that holds one.

  1. /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  2.  
  3. Returns the video id for any type of youtube url that holds one, like the links bellow.
  4.  
  5. http://www.youtube.com/watch?v=pkPgYbdQ1kQ&feature=feedu
  6. www.youtube.com/watch?v=pkPgYbdQ1kQ&feature=feedu
  7. youtube.com/watch?v=pkPgYbdQ1kQ&feature=feedu
  8. http://www.youtube.com/v/pkPgYbdQ1kQ&feature=feedu
  9. www.youtube.com/v/pkPgYbdQ1kQ&feature=feedu
  10. http://youtu.be/pkPgYbdQ1kQ
  11. youtu.be/pkPgYbdQ1kQ
  12.  
  13. |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
  14.  
  15. function uTubeId($url){
  16. $url = preg_replace('((http://)?(www.)?(youtube.com|youtu.be)(/v/|/watch\?v=|/)([a-zA-Z0-9_-]*)(.*))',"$5",$url);
  17. return (preg_match('(([a-zA-Z0-9_-]*){11,18})',$url) ? $url : '');
  18. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: fris on April 25, 2011

you should add ajax'd urls ;)

You need to login to post a comment.