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
Share on other sites

<?php $test="mail.google.com";

$break=explode('.', $test);

$result=$break[1]."."$break[2];

echo $result;

?>

 

edit: this only works for first level domains tho I have to look up the other way of doing it thats a bit more reliable

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.