962 posts
    Has been part of the Envato Community for over 5 years Has sold $1,000+ on Envato Market Has been a beta tester for an Envato feature Has collected 10+ items on Envato Market
+5 more
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
    Has been part of the Envato Community for over 5 years Has referred 1+ members Has sold $1,000+ on Envato Market Has been a beta tester for an Envato feature
+11 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
    Has referred 1+ members Has sold $1,000+ on Envato Market Has collected 1+ items on Envato Market Sells items exclusively on Envato Market
+2 more
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
    Has referred 1+ members Has sold $1,000+ on Envato Market Has collected 1+ items on Envato Market Sells items exclusively on Envato Market
+2 more
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
    Has been part of the Envato Community for over 5 years Has referred 1+ members Has sold $1,000+ on Envato Market Has been a beta tester for an Envato feature
+11 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
    Has been part of the Envato Community for over 5 years Has sold $1,000+ on Envato Market Has been a beta tester for an Envato feature Has collected 10+ items on Envato Market
+5 more
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
    Has referred 1+ members Has sold $1,000+ on Envato Market Has collected 1+ items on Envato Market Sells items exclusively on Envato Market
+2 more
theninja says
Try without the brackets :)
$search = $_POST['search'];
962 posts
    Has been part of the Envato Community for over 5 years Has sold $1,000+ on Envato Market Has been a beta tester for an Envato feature Has collected 10+ items on Envato Market
+5 more
webdesigndeluxe says

Still not working. Always getting the error message :)

296 posts
    Has referred 1+ members Has sold $1,000+ on Envato Market Has collected 1+ items on Envato Market Sells items exclusively on Envato Market
+2 more
theninja says

Still not working. Always getting the error message :)

What does the error message say?

962 posts
    Has been part of the Envato Community for over 5 years Has sold $1,000+ on Envato Market Has been a beta tester for an Envato feature Has collected 10+ items on Envato Market
+5 more
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