ThemeForest

Quick input form help.

962 posts
  • Has been a member for 2-3 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Grew a moustache for the Envato Movember competition
  • Beta Tester
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • 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!

2 years ago
4324 posts
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Contributed a Blog Post
  • Interviewed on the Envato Notes blog
  • Grew a moustache for the Envato Movember competition
  • Community Moderator
  • Beta Tester
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • United Kingdom
  • Referred between 1 and 9 users
Reaper-Media 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 :-)

2 years ago
Reaper-Media is a moderator
296 posts
  • Has been a member for 3-4 years
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 1 and 9 items
  • United Kingdom
  • Referred between 1 and 9 users
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!');
}
2 years ago
296 posts
  • Has been a member for 3-4 years
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 1 and 9 items
  • United Kingdom
  • Referred between 1 and 9 users
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

2 years ago
4324 posts
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Contributed a Blog Post
  • Interviewed on the Envato Notes blog
  • Grew a moustache for the Envato Movember competition
  • Community Moderator
  • Beta Tester
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • United Kingdom
  • Referred between 1 and 9 users
Reaper-Media 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

2 years ago
Reaper-Media is a moderator
962 posts
  • Has been a member for 2-3 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Grew a moustache for the Envato Movember competition
  • Beta Tester
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • 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!

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

Still not working. Always getting the error message :)

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

Still not working. Always getting the error message :)

What does the error message say?

2 years ago
962 posts
  • Has been a member for 2-3 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Grew a moustache for the Envato Movember competition
  • Beta Tester
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • 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

2 years ago
by
by
by
by
by