Mezelderz
Mezelderz

Reputation: 131

Youtube link and get method

Trying not to go to youtube for every video I find on some other site, and that is from youtube. If I right click and copy video URL, url will have this feature stuff inside the link, I am trying to get rid of that with str_replace.

Link http://www.youtube.com/watch?feature=player_embedded&v=wq7ftOZBy0E

$_GET['link'] = str_replace('feature=player_embedded&', '', $_GET['link']);

or

$_GET['link'] = str_replace('http://www.youtube.com/watch?feature=player_embedded&', 'http://www.youtube.com/watch?', $_GET['link']);

Either will return only http://www.youtube.com/watch?feature=player_embedded, instead of what I am expecting: http://www.youtube.com/watch?v=wq7ftOZBy0E

UPDATE:

I realized this is not problem in str_replace as I thought on first, looks like if url is

http://localhost/mysite/link.php/?link=http://www.youtube.com/watch?feature=player_embedded&v=wq7ftOZBy0E

$_GET['link'] uses url till "&". It is missing all this &v=wq7ftOZBy0E.

Seems like ampersand broke $_GET['link'], what can I do to make this work?

Solution, based on accepted answer:

if(isset($_GET['v']))
{
$_GET['link'] = $_GET['link'].'&v='.$_GET['v'];
$_GET['link'] = str_replace('feature=player_embedded&', '', $_GET['link']);
$_GET['link'] = str_replace('http://www.youtube.com/watch?v=', '//www.youtube.com/embed/', $_GET['link']);
$_GET['link'] = str_replace('www.youtube.com/watch?v=', '//www.youtube.com/embed/', $_GET['link']);
$_GET['link'] = str_replace('http://youtu.be/', '//www.youtube.com/embed/', $_GET['link']);
echo $_GET['link'];
}
else
{
$_GET['link'] = str_replace('http://www.youtube.com/watch?v=', '//www.youtube.com/embed/', $_GET['link']);
$_GET['link'] = str_replace('www.youtube.com/watch?v=', '//www.youtube.com/embed/', $_GET['link']);
$_GET['link'] = str_replace('http://youtu.be/', '//www.youtube.com/embed/', $_GET['link']);
echo $_GET['link'];
}

Upvotes: 0

Views: 281

Answers (4)

Pavan Kumar
Pavan Kumar

Reputation: 406

Just add the following code

if(isset($_GET['v']))
{
$resultURL=$_GET['link'].'&v='.$_GET['v'];
}

Upvotes: 1

Pavan Kumar
Pavan Kumar

Reputation: 406

You can get the data which is after & as $_GET['v'].

Which simply gets the data assigned to the variable 'v'

Upvotes: 0

Vishal
Vishal

Reputation: 553

  $id =  substr($_GET['link'],-11);

This will give you the youtube video id (the unique string after 'v=-------',Youtube ids are 11 digit random strings).

Upvotes: 0

Pavan Kumar
Pavan Kumar

Reputation: 406

I run this code

<?php
$_GET['link']="http://www.youtube.com/watch?feature=player_embedded&v=wq7ftOZBy0E";

echo $_GET['link'] = str_replace('feature=player_embedded&', '', $_GET['link']);

?>

And i got the following ouput

http://www.youtube.com/watch?v=wq7ftOZBy0E

I dont know what you are looking for

Upvotes: 0

Related Questions