$_GET converts characters!

Hi, I want to create a PHP script to highlight “v” variables in Youtube URLs.

For example, I want to highlight L0otYijxNbc in the following URL:

www.youtube.com/watch?v=L0otYijxNbc&feature=youtube_gdata_player

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.

http://www.mywebsite.com/checkyoutubeid.php?youtubeurl=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DL0otYijxNbc%26feature%3Dyoutube_gdata_player&Submit=SEND

Any help would be most appreciated,

Leao

I’m gonna guess you need to take the incoming url and apply function urldecode to it. Look it up in the php manual.

Hi Leao,

You could use [fphp]parse_url[/fphp] and [fphp]parse_str[/fphp] to do something like this:


$array = parse_url($_GET['youtubeurl']);
parse_str($array['query'], $output);

echo 'The Youtube ID of the video is: ' . $output['v'];

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";
  }

Thanks again for your help,

Leao