Posted By

mladoux on 08/02/11


Tagged

embed YouTube


Versions (?)

Embed Youtube


 / Published in: PHP
 

URL: http://markladoux.com/

Simple script to embed YouTube videos.

  1. <?php
  2. function embed_youtube ($url)
  3.  
  4. {
  5.  
  6. // split up string
  7.  
  8.  
  9. $parts = array_merge(array(), array_filter(explode('/', $url)));
  10.  
  11. // clean parts
  12. if(stripos($parts['1'], 'www') !== false)
  13. {
  14. $parts['1'] = substr($parts['1'], 4);
  15. }
  16.  
  17. // parse link
  18.  
  19. switch($parts[1])
  20. {
  21. case 'youtu.be':
  22. $video = $parts[2];
  23. break;
  24. case 'youtube.com':
  25. $raw = array_merge(array(), array_filter(explode("?", $parts[2])));
  26. $qs = array_merge(array(), array_filter(explode("&", $raw[1])));
  27.  
  28. $qp = array();
  29.  
  30. foreach($qs as $value) {
  31. $qi = array_merge(array(), array_filter(explode("=", $value)));
  32. $qp[$qi[0]] = $qi[1];
  33. }
  34.  
  35. $video = $qp['v'];
  36. break;
  37. default:
  38. $video = FALSE;
  39. }
  40.  
  41. // build embed string
  42. ?>
  43. <iframe
  44. width="560"
  45. height="349"
  46. src="http://www.youtube.com/embed/<?php echo $video; ?>"
  47. frameborder="0"
  48. allowfullscreen>
  49. </iframe>
  50. <?php
  51.  
  52. }
  53. ?>

Report this snippet  

You need to login to post a comment.