title: Validate domain name PHP
body: <p>I need to validate domain names using PHP.<br>
  Only this format should be allowed: <code>google.com</code>, <code>subdomain.stack-overflow.org</code> etc<br>
  It should only allow - (in case of special characters)</p>
  
  <p>No other format should be allowed ex: <code>http, https,?,/index.php,www.</code> etc (none of these should be allowed).</p>
  
  <p>Also i do not want to allow: <code>localhost</code>,<code>mydomain.com</code></p>
  
  <p>I have tried: <code>FILTER_VALIDATE_URL</code> but it accepts <code>http</code> etc.<br>
  I also tried something regular expression like this:  <code>[a-z0-9-]+(.[a-z0-9-]+)</code></p>
  
  <p>But it is allowing ex: <code>stackoverflow</code>  (without a <code>.org</code>).</p>
  
  <p>How can I achieve that?</p>
  
  <pre><code>$urlparts = parse_url(filter_var($url, FILTER_SANITIZE_URL));
  if(!isset($urlparts['host'])){
      $urlparts['host'] = $urlparts['path'];
  }
  </code></pre>