title: Strip string containing URL to the domain, and include subdomain if available
body: <p>Currently, I can strip the domain of the URL string by:</p>
  
  <pre><code>$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
  $url = 'http://www.example.com/foo/bar?hat=bowler&amp;accessory=cane';
  if (preg_match($pattern, $url, $matches) === 1) {
      echo $matches[0];
  }
  </code></pre>
  
  <p>This will echo:</p>
  
  <pre><code>example.com
  </code></pre>
  
  <p>The problem is that I want to include the subdomain <strong>if it exists</strong>. </p>
  
  <p>So for example:</p>
  
  <pre><code>$url = 'http://sub.domain.example.com/foo/bar?hat=bowler&amp;accessory=cane';
  </code></pre>
  
  <p>Should echo:</p>
  
  <pre><code>sub.domain.example.com
  </code></pre>
  
  <p>How can I achieve this insanity?</p>