title: Validating Youtube URL using Regex
body: <p>I'm trying to validate YouTube URLs for my application.</p>
  
  <p>So far I have the following:</p>
  
  <pre><code>// Set the youtube URL
  $youtube_url = "www.youtube.com/watch?v=vpfzjcCzdtCk";
  
  if (preg_match("/((http\:\/\/){0,}(www\.){0,}(youtube\.com){1} || (youtu\.be){1}(\/watch\?v\=[^\s]){1})/", $youtube_url) == 1)
  {
      echo "Valid";
  else
  {
      echo "Invalid";
  }
  </code></pre>
  
  <p>I wish to validate the following variations of Youtube Urls:</p>
  
  <ul>
  <li>With and without http://</li>
  <li>With and without www.</li>
  <li>With the URLs youtube.com and youtu.be</li>
  <li>Must have /watch?v=</li>
  <li>Must have the unique video string (In the example above "vpfzjcCzdtCk")</li>
  </ul>
  
  <p>However, I don't think I've got my logic right, because for some reason it returns <strong>true</strong> for: <code>www.youtube.co/watch?v=vpfzjcCzdtCk</code> (Notice I've written it incorrectly with <code>.co</code> and not <code>.com</code>)</p>