1. $youtubeVal=http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage
  2.  
  3. var regExp = /^.*(youtu.be/|v/|u/w/|embed)([^#&?]*).*/;
  4. var match = url.match(regExp);
  5. if (match&&match[2].length==11){
  6. return match[2];
  7. }
  8.  
  9. youtubeVal.substring(29, 39);
  10.  
  11. function GetYoutubeID($url) {
  12. $temp = parse_url($url);
  13.  
  14. if(isset($temp['query'])) {
  15. parse_str($temp['query'], $temp2);
  16. if(isset($temp2['v'])) {
  17. return $temp2['v'];
  18. }
  19. }
  20.  
  21. if(isset($temp['path'])) {
  22. $temp2 = explode("/", $temp['path']);
  23. foreach($temp2 as $value) {
  24. if(strlen($value) == 11 || strlen($value) == 10) {
  25. return $value;
  26. }
  27. }
  28. }
  29.  
  30. return "no ID?";
  31. }
  32.  
  33. echo $youtubeID = GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage') . "n";
  34. echo $youtubeID = GetYoutubeID('https://www.youtube.com/embed/6FjfewWAGdE') . "n";
  35. echo $youtubeID = GetYoutubeID('https://youtube.com/embed/6FjfewWAGdE') . "n";
  36. echo $youtubeID = GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE') . "n";
  37. echo $youtubeID = GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage') . "n";
  38. echo $youtubeID = GetYoutubeID('www.youtu.be/6FjfewWAGdE') . "n";
  39. echo $youtubeID = GetYoutubeID('youtu.be/6FjfewWAGdE') . "n";
  40. echo $youtubeID = GetYoutubeID('youtube.com/watch?v=6FjfewWAGdE') . "n";
  41. echo $youtubeID = GetYoutubeID('https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be') . "n";
  42.  
  43. preg_match('/^(http://|https://|.*?)(www.|.*?)(youtube.com|youtu.be)/(embed/|watch?v=|.*?)(.*?)(?|&|$)/is', $url, $matches);
  44. if(isset($matches[5])) {
  45. return $matches[5];
  46. }
  47.  
  48. return "no ID?"
  49.  
  50. function GetYoutubeID(url) {
  51. var regExp = /^(http://|https://|.*?)(www.|.*?)(youtube.com|youtu.be)/(embed/|watch?v=|.*?)(.*?)(?|&|$)/;
  52. var match = url.match(regExp);
  53. if (match){
  54. return match[5];
  55. }
  56.  
  57. return "no ID?";
  58. }
  59.  
  60. console.log(GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage'));
  61. console.log(GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage'));
  62. console.log(GetYoutubeID('https://www.youtube.com/embed/6FjfewWAGdE'));
  63. console.log(GetYoutubeID('https://youtube.com/embed/6FjfewWAGdE'));
  64. console.log(GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE'));
  65. console.log(GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage'));
  66. console.log(GetYoutubeID('www.youtu.be/6FjfewWAGdE'));
  67. console.log(GetYoutubeID('youtu.be/6FjfewWAGdE'));
  68. console.log(GetYoutubeID('youtube.com/watch?v=6FjfewWAGdE'));
  69. console.log(GetYoutubeID('https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be'));