$youtubeVal=http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage var regExp = /^.*(youtu.be/|v/|u/w/|embed)([^#&?]*).*/; var match = url.match(regExp); if (match&&match[2].length==11){ return match[2]; } youtubeVal.substring(29, 39); function GetYoutubeID($url) { $temp = parse_url($url); if(isset($temp['query'])) { parse_str($temp['query'], $temp2); if(isset($temp2['v'])) { return $temp2['v']; } } if(isset($temp['path'])) { $temp2 = explode("/", $temp['path']); foreach($temp2 as $value) { if(strlen($value) == 11 || strlen($value) == 10) { return $value; } } } return "no ID?"; } echo $youtubeID = GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage') . "n"; echo $youtubeID = GetYoutubeID('https://www.youtube.com/embed/6FjfewWAGdE') . "n"; echo $youtubeID = GetYoutubeID('https://youtube.com/embed/6FjfewWAGdE') . "n"; echo $youtubeID = GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE') . "n"; echo $youtubeID = GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage') . "n"; echo $youtubeID = GetYoutubeID('www.youtu.be/6FjfewWAGdE') . "n"; echo $youtubeID = GetYoutubeID('youtu.be/6FjfewWAGdE') . "n"; echo $youtubeID = GetYoutubeID('youtube.com/watch?v=6FjfewWAGdE') . "n"; echo $youtubeID = GetYoutubeID('https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be') . "n"; preg_match('/^(http://|https://|.*?)(www.|.*?)(youtube.com|youtu.be)/(embed/|watch?v=|.*?)(.*?)(?|&|$)/is', $url, $matches); if(isset($matches[5])) { return $matches[5]; } return "no ID?" function GetYoutubeID(url) { var regExp = /^(http://|https://|.*?)(www.|.*?)(youtube.com|youtu.be)/(embed/|watch?v=|.*?)(.*?)(?|&|$)/; var match = url.match(regExp); if (match){ return match[5]; } return "no ID?"; } console.log(GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage')); console.log(GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage')); console.log(GetYoutubeID('https://www.youtube.com/embed/6FjfewWAGdE')); console.log(GetYoutubeID('https://youtube.com/embed/6FjfewWAGdE')); console.log(GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE')); console.log(GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage')); console.log(GetYoutubeID('www.youtu.be/6FjfewWAGdE')); console.log(GetYoutubeID('youtu.be/6FjfewWAGdE')); console.log(GetYoutubeID('youtube.com/watch?v=6FjfewWAGdE')); console.log(GetYoutubeID('https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be'));