Youtube Video ID Regex & PHP Function


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.