Jump to content

[SOLVED] Stripping subdomain from url


Recommended Posts

Hello.

 

I have a form for where a domain is submitted. Although i only need the base domain so no subdomains.

 

I'm guessing it will be some kind of str_replace or preg_match but i'm unsure what it would be.

 

So if for example mail.google.com was submitted the value i want is google.com, and if google.com is submitted i want google.com.

 

Thanks for any advice/code.

Link to comment
https://forums.phpfreaks.com/topic/161273-solved-stripping-subdomain-from-url/
Share on other sites

Have a look at parse_url().

 

Edit: Actually, it doesn't seem like the host-only is returned by that function. Using explode():

 

<?php
$str = 'mail.google.com';
$parts = array_reverse(explode('.', $str));
$host = "{$parts[1]}.{$parts[0]}";
?>

By reversing the array we get around the problem phpdragon ran into.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.