Extract domain or subdomain PHP
<p>I need to extract either domain or 1st level subdomain from the link with regex</p>

<pre><code>www.google.com -&gt; google.com
www.maps.google.com -&gt; maps.google.com
www.dev.maps.google.com - &gt; maps.google.com
www.dev.maps.google.com/show/maps - &gt; maps.google.com
</code></pre>

<p>I know how to do extract just the domain, but I'm struggling to write an expression that would detect if there are subdomains and get only the first one
Could someone show me the right expression?</p>