php parse youtube url’s to resize and remove related links..
I often get asked about you tube embed code by clients and just had a customer that wanted to be able to resize the videos himself and remove the links to related videos. I tried to explain the embed code but decided afterwards I would make it easier for them with a form and as sharing is caring here is the code..
How to resize you tube videos from the url without the embed code and remove links to related videos with php.
The first step is to make the form. The code bellow will do just that.
-
-
<form action="process.php" method="post">
-
Url:
-
<input name="id" type="text" />
-
Width:
-
<input name="width" type="text" />
-
Height:
-
<input name="height" type="text" />
-
<input type="submit" value="submit" />
-
</form>
-
Next is the processing part we will take the values from the form and use them as variables to insert in to our custom youtube embed code. This script will parse the youtube url and extract the video id for the embed code. This file will be named process.php
-
-
<?php
-
//parse the id from the url
-
$url = $_POST[‘id’];
-
$width = $_POST[‘width’];
-
$height = $_POST[‘height’];
-
$vidid = ($query[‘v’]);
-
-
//check if its valid
-
if ($vidid == "") {
-
echo "That does not seem to be a valid youtube video address please check your address</p>";
-
}
-
else {
-
-
//get the video id in to the embed code
-
$content = "<object width=\"".$width."\" height=\"".$height."\"><param name=\"movie\" value=\"http://www.youtube.com/v/".$vidid."&hl=en&fs=1&rel=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"http://www.youtube.com/v/".$vidid."&hl=en&fs=1&rel=0\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"".$width."\" height=\"".$height."\"></embed></object>";
-
Now we have the embed code we can write it to a text file with the code bellow.
-
-
//open and write to the vid file
-
$myfile = "video.txt";
-
$Data = $content;
-
?>
-
And to add it to your site.
-
-
<?php include("video.txt");?>
-
And thats it put the form on a password protected page and your client can easilly update there videos.
You can see it in action by entering the Url of any you tube video in the box bellow!!



























































