Monday, 1 February 2016

YouTube HTML 5 Embed iframe code from URL with PHP

Hi folks
I was making a website and my client request me to upload youtube video on his website. For do this take some fact about youtube video



  • When upload video. Youtube provide a unique URL for every uploaded video which user can use to share their video to other platform or other youtube user.
  • Iframe is used to show youtube video on other website
  • An embedded URL need in iframe SRC
Copy paste following complete code on your website as your requirement.

<?php 
$msg="";
 $id="";
 $width="";
 $height="";
if($_POST['n'])
{
$url = $_POST['url'];
 preg_match('/[\\?\\&]v=([^\\?\\&]+)/', $url, $matches);
 $id = $matches[1];
 $width = '400px';
 $height = '250px';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Girfa : Youtube embed video demo</title>
</head>

<body>

<form method="post"  action=<?php echo "$_SERVER[PHP_SELF]" ?>  >
        Enter youtube video URL : <input type="text" name="url" />
            <input type="submit" value="upload"  name="n" />
            
        </form>
        <br /><br />
<iframe id="ytplayer" type="text/html" width="<?php echo $width ?>" height="<?php echo $height ?>"
    src="https://www.youtube.com/embed/<?php echo $id ?>?rel=0&showinfo=0&color=white&iv_load_policy=3"
    frameborder="0" allowfullscreen style="border:solid 2px #666666;padding:5px;border-radius:5px;box-shadow: 5px 5px 5px #000"></iframe> 
</body>
</html>