I see what I did wrong and I rewrote the code. Everything is working except for when no matches are found. It returns a value of 0, but it also shows a notice on the screen now if no matches are found : Notice: Undefined index: host in C:\xampp\htdocs\functions\common.php on line 14
Everything is working, but if a user types something that doesn't get matched I do not want this notice to be displayed.
With the new code, how can I remove the notice?
New function :
function getYoutubeId($ytURL)
{
$urlData = parse_url($ytURL);
//echo '<br>'.$urlData["host"].'<br>';
if($urlData["host"] == 'www.youtube.com') // Check for valid youtube url
{
$ytvIDlen = 11; // This is the length of YouTube's video IDs
// The ID string starts after "v=", which is usually right after
// "youtube.com/watch?" in the URL
$idStarts = strpos($ytURL, "?v=");
// In case the "v=" is NOT right after the "?" (not likely, but I like to keep my
// bases covered), it will be after an "&":
if($idStarts === FALSE)
$idStarts = strpos($ytURL, "&v=");
// If still FALSE, URL doesn't have a vid ID
if($idStarts === FALSE)
die("YouTube video ID not found. Please double-check your URL.");
// Offset the start location to match the beginning of the ID string
$idStarts +=3;
// Get the ID string and return it
$ytvID = substr($ytURL, $idStarts, $ytvIDlen);
return $ytvID;
}
else
{
return 0;
}
}
New php code :
<?php
include 'functions/common.php';
include 'dbconnect.php';
//if(isset($_POST['submit']) )
//{
$ytURL = "33";
$youtubeid = getYoutubeId($ytURL);
echo $youtubeid;
?>