Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The function <a href="http://php.net/parse_url" rel="nofollow noreferrer"><code>parse_url()</code></a> requires a valid URL. In this case <code>test.web.tv</code> isn't valid, so <a href="http://php.net/parse_url" rel="nofollow noreferrer"><code>parse_url()</code></a> won't give you the expected results. In order to get around this, you could first check if the URL has the <code>http://</code> prefix, and if it doesn't, manually prepend it. That way, you can get around the limitation of <a href="http://php.net/parse_url" rel="nofollow noreferrer"><code>parse_url()</code></a>.</p> <p>However, I think it'd be better to use the following function. </p> <pre><code>function getDomain($url) { if (!preg_match("~^(?:f|ht)tps?://~i", $url)) { $url = "http://" . $url; } $domain = implode('.', array_slice(explode('.', parse_url($url, PHP_URL_HOST)), -2)); return $domain; } </code></pre> <p>Explanation:</p> <ul> <li>The given URL's passed to <a href="http://php.net/parse_url" rel="nofollow noreferrer"><code>parse_url()</code></a> with the <code>PHP_URL_HOST</code> flag and the full host is obtained</li> <li>It's exploded with <code>.</code> as a delimiter</li> <li>The last two pieces of the array is sliced -- ie. the domain name</li> <li>It's joined back using <a href="http://php.net/implode" rel="nofollow noreferrer"><code>implode()</code></a></li> </ul> <p>Test:</p> <pre><code>echo getDomain('test.web.tv'); </code></pre> <p>Output:</p> <pre><code>web.tv </code></pre> <p><a href="http://codepad.org/nMHmiK40" rel="nofollow noreferrer"><strong>Demo!</strong></a></p> <p>Note: It's a modified version of my own answer <a href="https://stackoverflow.com/a/18881399/1438393">here</a> combined with Alix's answer <a href="https://stackoverflow.com/questions/2762061/">here</a>.</p> <p>This function currently doesn't work for <code>.co.uk</code> domain extensions -- you can easily add a check and change the <code>array_slice</code> function accordingly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload