I have created a simple form where you can enter a Youtube address and the following page spits out the “v” variable contained within the Youtube URL.
FORM:
<form action="http://www.mywebsite.com/checkyoutubeid.php" method="get">
Please enter the URL of the Youtube video: <input type="text" name="youtubeurl" size="43" ><br/>
<input type='submit' name='Submit' value='SEND' />
</form>
RESULTING PAGE:
The Youtube ID of the video is <?php echo $_GET["v"]; ?>
The problem is that all the symbols in the submitted URL get converted. For example, ‘&’ turns to ‘%3F’ and ‘=’ turns to ‘%3D’, meaning the PHP code can’t do its job.
Thanks for your help. I used your code and added a couple of conditions (see below).
$userword = $_GET['youtubeurl'];
$array = parse_url($_GET['youtubeurl']);
parse_str($array['query'], $output);
$vidid = $output['v'];
if (strpos($userword,'user') !== false)
{
echo "INVALID YOUTUBE ADDRESS
<br/><br/>
You entered the URL of a Youtube channel rather than a specific video.";
}
//The above condition checks if the Youtube URL contains the word 'user'.
//If it does it means that the submitted URL is probably a Youtube channel page rather than a specific video.
elseif (empty($vidid))
{
echo "YOU HAVEN'T ENTERED A VALID YOUTUBE ADDRESS";
}
//This condition checks if the Youtube URL contains the 'v' variable.
//If the submitted URL doesn't contain a 'v' variable it is most probably not a Youtube URL.
else
{
echo "Your video's ID is: $vidid";
}