Posted By

Jamie on 08/05/10


Tagged

regex php video thumbnail YouTube


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

LeeRJohnson
beertigger
manavecplan
Onfire60
Balamir


Youtube Video ID Regex & PHP Function


 / Published in: PHP
 

I\'ve added an additional case to handle videos linked from a user\'s channel.

  1. /**
  2.  * Creates a YouTube thumbnail URL from a YouTube video URL
  3.  */
  4. function youtube_video_thumbnail($url,$index=0){
  5. if(preg_match('/^[^v]+v.(.{11}).*/',$url,$matches)){
  6. return 'http://img.youtube.com/vi/'.$matches[1].'/'.$index.'.jpg';
  7. }elseif(preg_match('/youtube.com\/user\/(.*)\/(.*)$/',$url,$matches)){
  8. return 'http://img.youtube.com/vi/'.$matches[2].'/'.$index.'.jpg';
  9. }else{
  10. return false;
  11. }
  12. }

Report this snippet  

You need to login to post a comment.