962 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 1 000 and 5 000 dollars
  • United States
webdesigndeluxe says

Hey everyone, I have a simple problem that needs a solution. I have an input field where the user would enter a youtube link like so:

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

I need some type of JavaScript or PHP code to simply remove the:

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

so all you have is the code to the video.

Hopefully that makes since :) Any help would be much appreciated. Thanks!

4335 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Community Moderator
  • Contributed a Blog Post
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
+6 more
Reaper-Media Volunteer moderator says
PHP :
function retreiveYoutubeCode($url){
    return return preg_replace('/http\:\/\/[w,\.]*youtube\.[a-z]*\/watch[\/]*\?v\=(a-z0-9)*/i','$1',$url);        
}
echo retreiveYoutubeCode('http://www.youtube.com/watch?v=TG7RJZUwR6Q');

This doesn’t take into account other get variables, i’ll take a look at this now :-)

296 posts
  • Exclusive Author
  • Has been a member for 5-6 years
  • Sold between 1 000 and 5 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
  • United Kingdom
theninja says
Regex to the rescue!

In PHP you could do something like this :
$url = 'http://www.youtube.com/watch?v=TG7RJZUwR6Q';
if(preg_match('#http://(?:www\.)?youtube[\.a-z]{1,6}/watch.*[\?&]v=([^\?&/]{11})#i', $url, $match)) {
    echo $match[1]; // $match[1] is your video id
} else {
    echo 'Not a valid youtube url dude!';
}

I think this covers most situations… like if they don’t use www or if they use a .co.uk youtube link or if there are other query strings before the video id (I doubt there are but best to be safe than sorry). You could do the same with JavaScript too, since it also supports all the the regex I used in the PHP version. :D
Simple JavaScript example :
var url = 'http://www.youtube.com/watch?v=TG7RJZUwR6Q',
    match = url.match(/http:\/\/(?:www\.)?youtube[\.a-z]{1,6}\/watch.*[\?&]v=([^\?&\/]{11})/i);

if(match) {
    alert(match[1]);
} else {
    alert('Not a valid youtube url dude!');
}
296 posts
  • Exclusive Author
  • Has been a member for 5-6 years
  • Sold between 1 000 and 5 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
  • United Kingdom
theninja says

PHP :
function retreiveYoutubeCode($url){
    return return preg_replace('/http\:\/\/[w,\.]*youtube\.[a-z]*\/watch[\/]*\?v\=(a-z0-9)*/i','$1',$url);        
}
echo retreiveYoutubeCode('http://www.youtube.com/watch?v=TG7RJZUwR6Q');
This doesn’t take into account other get variables, i’ll take a look at this now :-)

Haha you beat me to it Sam! :P

4335 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Community Moderator
  • Contributed a Blog Post
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
+6 more
Reaper-Media Volunteer moderator says


PHP :
function retreiveYoutubeCode($url){
    return return preg_replace('/http\:\/\/[w,\.]*youtube\.[a-z]*\/watch[\/]*\?v\=(a-z0-9)*/i','$1',$url);        
}
echo retreiveYoutubeCode('http://www.youtube.com/watch?v=TG7RJZUwR6Q');
This doesn’t take into account other get variables, i’ll take a look at this now :-)
Haha you beat me to it Sam! :P

Yeah but your solution works fully dude ;) :P Also you included multi domain extensions and i only included one :P

962 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 1 000 and 5 000 dollars
  • United States
webdesigndeluxe says

Okay so hopefully this isn’t too stupid of a question to ask… but how do I get this to work with the input value? My form action would be submit.php and I have it looking like so:

<?

$search = ($_POST['search']);

if(preg_match('#http://(?:www\.)?youtube[\.a-z]{1,6}/watch.*[\?&]v=([^\?&/]{11})#i', $search, $match)) {
    echo $match[1]; // $match[1] is your video id
} else {
    echo 'Not a valid youtube url dude!';
}
?>

What am I doing wrong? :)

And both solutions are great! Thanks!

296 posts
  • Exclusive Author
  • Has been a member for 5-6 years
  • Sold between 1 000 and 5 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
  • United Kingdom
theninja says
Try without the brackets :)
$search = $_POST['search'];
962 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 1 000 and 5 000 dollars
  • United States
webdesigndeluxe says

Still not working. Always getting the error message :)

296 posts
  • Exclusive Author
  • Has been a member for 5-6 years
  • Sold between 1 000 and 5 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
  • United Kingdom
theninja says

Still not working. Always getting the error message :)

What does the error message say?

962 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 1 000 and 5 000 dollars
  • United States
webdesigndeluxe says


Still not working. Always getting the error message :)
What does the error message say?

It was your error message. But no worries it works now. My dumb mistake. Never put method=”post” in the form :D

by
by
by
by
by
by