esedic
11/20/2014 - 5:24 PM

Responsive youtube video embed with PHP

Responsive youtube video embed with PHP

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>Online PHP Script Execution</title>
<link href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css' rel='stylesheet' type='text/css'>
<style>
body {padding: 1em;}
.video-container {position: relative;padding-bottom: 56.25%;padding-top: 30px;height: 0;overflow: hidden;}
.video-container iframe, .video-container object, .video-container embed {position: absolute;top: 0;left: 0;width: 100%;height: 100%;border: none;}
</style>
</head>

<body>
  <div class="container">
    <div class="row">
      <div class="col-md-12">
      <?php
        // define item
        $videocode  = '<iframe width="640" height="360" src="//www.youtube.com/embed/VazPRr8edZw" frameborder="0" allowfullscreen></iframe>';
        // array of strings which should be replaced
        $before = array('<iframe', '</iframe>');
        // array of substitute strings
        $after = array('<div class="video-container"><iframe', '</div></iframe>');
        // construct new item
        $respvideo = str_replace($before, $after, $videocode);
        // display responsive video
        echo $respvideo;
      ?>
  
      </div>
    </div>
  </div>

</body>
</html>