title: Ultimate URL and Social Network URL Validation
body: <p>I've found a validation for websites. But later I needed to make some changes and to add special Social Networks (facebook, twitter, plus.google) validation.</p>
  
  <p>This is what I've got:</p>
  
  <pre><code>function isValidUrl($url,$media) {
  
  $url= strtolower($url);
  // Scheme
  $urlregex = "^(https?)\:\/\/";
  // User and Pass (optional)
  if (!isset($media)) {
      $urlregex .= "([A-Za-z0-9+!*(),;?&amp;=\$_.-]+(\:[A-Za-z0-9+!*(),;?&amp;=\$_.-]+)?@)?";
  }
  // Hostname
  if (isset($media)) {
      if ($media == 'fb') { $urlregex .= "([facebook]+\.)"; }
      else if ($media == 'gplus') { $urlregex .= "([plus\.google]+\.)"; }
      else if ($media == 'twitter') { $urlregex .= "([twitter]+\.)"; }
  } else {
      $urlregex .= "([A-Za-z0-9+\$_-]+\.)";
  }
  $urlregex .= "*(?:[A-Za-z]{2}|com";
  if (!isset($media)) {
      $urlregex .= "|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum|cat|coop|int|pro|tel|travel|xxx";
  }
  // Hostname End
  $urlregex .= ")";
  // Port/Path (optional)
  $urlregex .= "(\:[0-9]{2,5})?";
  $urlregex .= "(\/([A-Za-z0-9+\$_-]\.?)+)*\/?";
  // Query
  $urlregex .= "(\?[A-Za-z+&amp;\$_.-][A-Za-z0-9;:@/&amp;%=+\$_.-]*)?";
  // Anchor
  $urlregex .= "(#[A-Za-z_.-][A-Za-z0-9+\$_.-]*)?\$^";
  return preg_match($urlregex,$url);
  
  }
  </code></pre>
  
  <p>Simple website is validating alright, but Social Network isn't in the way I want.
  For example <code>http://facebook.com</code> is valid, but I need to be valid an URL like <code>http://facebook.com/something</code>, and to make the first one to become unvalid (same for <code>http://twitter.com</code> and <code>http://plus.google.com</code>). The <code>http://plus.google.com</code> validation doesn't work, it allows <code>http://plusgoogle.com</code> and other fusions.</p>
  
  <p>What I'd like to <strong>change/add</strong>:</p>
  
  <p>1) Fix Social Networks (facebook, gplus, twitter) validation as described above;</p>
  
  <p>2) To also allow URLs without protocols <code>http://</code> or with them and <code>www.</code> both, so to make the link <code>http://stackoverflow.com</code> to become allowed in the ways <code>http://www.stackoverflow.com</code>, <code>stackoverflow.com</code> and <code>www.stackoverflow.com</code>.</p>
  
  <p><strong>EDIT:</strong> To make things clear, I call this function the way below.</p>
  
  <pre><code>$error = false;
  
  // For simple URL
  $url = $_POST['url'];
  if (!isValidURL($url,NULL)) { $error = true; }
  
  // For Facebook URL
  $fbpage = $_POST['fbpage'];
  if (!isValidURL($fbpage,'fb')) { $error = true; }
  
  // For Twitter URL
  $twitterpage = $_POST['twitterpage'];
  if (!isValidURL($twitterpage,'twitter')) { $error = true; }
  
  // For Google Plus URL
  $gpluspage = $_POST['gpluspage'];
  if (!isValidURL($gpluspage,'gplus')) { $error = true; }
  </code></pre>