Getting the domain name out of a URL

/ Published in: PHP
Save to your folder(s)

Copy this code and paste it in your HTML
  1. <?php
  2. // get host name from URL
  3. preg_match('@^(?:http://)?([^/]+)@i',
  4. "", $matches);
  5. $host = $matches[1];
  7. // get last two segments of host name
  8. preg_match('/[^.]+\.[^.]+$/', $host, $matches);
  9. echo "domain name is: {$matches[0]}\n";
  10. ?>

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.