Validate a Youtube URL for PHP and Javascript
I like the idea of posting random small useful things here, if only to help someone else along the way for whatever purpose. I think I'll do it more often if I can.
Here's a quick, but useful code snippet.
I'm working on a project that uses the Youtube API and I relies on a user to input a Youtube video URL, plus grab the video ID of that video from the URL.
Of course, since you don't want a user inputting a malformed address, it's necessary to validate that it is proper - ideally both on client-side for quick feedback, and on server-side for extra security.
The functions below use regular expressions to validate a youtube URL and returns the results in the form of an array.
PHP Example:
Here's a quick, but useful code snippet.
I'm working on a project that uses the Youtube API and I relies on a user to input a Youtube video URL, plus grab the video ID of that video from the URL.
Of course, since you don't want a user inputting a malformed address, it's necessary to validate that it is proper - ideally both on client-side for quick feedback, and on server-side for extra security.
The functions below use regular expressions to validate a youtube URL and returns the results in the form of an array.
PHP Example:
/*FUNCTION: validate_youtube_urlJavascript Example:
Returns: array(valid URI:Boolean, Error Message or Video ID:String, match results:Array)
*/
function validate_youtube_url($str, $protocol = '(http://)|(http://www.)|(www.)'){ // Default to requiring http://, http://www., or www. prefix
$protocol = str_replace('.', '\.', str_replace('/', '\/', $protocol)); // escape those reg exp characters
$protocol = ($protocol != '') ? '^(' . $protocol . ')' : $protocol; //if empty arg passed, let it it match anything at beginning
$match_str = '/' . $protocol . 'youtube\.com\/(.+)(v=.+)/'; //build the match string
preg_match($match_str, $str, $matches); // find the matches and put them in $matches variable
if(count($matches) < 3){ //No matter what protocol/prefix, we should have at least 3 matches
return array(false, 'Invalid URI', $matches); //bad URI
}else{ //so far so good....
$qs = explode('&',$matches[count($matches)-1]); //the last match will be the querystring - split them at amperstands
$vid = false; //default the video ID to false
for($i=0; $i<count($qs); $i++){ //loop through the params
$x = explode('=', $qs[$i]); //split at = to find key/value pairs
if($x[0] == 'v' && $x[1]){ //if the param is 'v', and it has a value associated, we want it
$vid = $x[1]; // set the video id to the val
return array(true, $vid, $matches);
}else{
return array(false, 'Missing ID', $matches); //invalid querystring - couldn't find the video ID
}
}
return array(false, false, false); //everything went wrong....ouch
}
}
$u = validate_youtube_url('http://youtube.com/watch?v=_ZSbC09qgLI&feature=rec-HM-rev-rn');
echo $u[1];
/*FUNCTION: validate_youtube_url
Returns: array(valid URI:Boolean, Error Message or Video ID:String, match results:Array)
*/
function validate_youtube_url(str, protocol){
if(!protocol && protocol != ''){
protocol = '(http://)|(http://www.)|(www.)';
}
protocol = protocol.replace(/\//g, '\/', protocol).replace(/\./g, '\.');
protocol = (protocol != '') ? '^(' + protocol + ')' : protocol;
match_exp = new RegExp(protocol + 'youtube\.com\/(.+)(v=.+)', 'gi');
var matches = match_exp.exec(str);
if(matches.length < 3){
return Array(false, 'Invalid URI', matches);
}else{
var qs = matches[matches.length-1].split('&');
var vid = false;
for(i=0; i<qs.length; i++){
var x = qs[i].split('=');
if(x[0] == 'v' && x[1]){
vid = x[1];
return Array(true, vid, matches);
}else{
return Array(false, 'Missing ID', matches);
}
}
return Array(false, false, false);
}
}
var u = validate_youtube_url('http://youtube.com/watch?v=_ZSbC09qgLI&feature=rec-HM-rev-rn');
alert(u[1]);
0 Comments:
Post a Comment
Links to this post:
Create a Link
<< Home