Leifus
Leifus

Reputation: 41

how to remove everything before second last dot in a string

I am trying to make a function that removes everything before the second to last dot. If the string is something like www.dev.community.google.com then I want to remove everything before google.com.

These sub domains aren't always alike, sometimes they will just be www.google.com and sometimes www.community.google.com.

So the point is to remove everything before and including the second to last dot. How can I do this with php?

Upvotes: 1

Views: 934

Answers (4)

axiac
axiac

Reputation: 72177

Another solution that uses regexp to identify and keep only the last two components:

function domain($input)
{
    return preg_replace('/^.*\.([^.]*\.[^.]*)$/', '\1', $input);
}

The regular expression is nothing special:

^                  # matches the start of the string
.*                 # anything, any number of times
\.                 # followed by a dot (need to escape it to get its literal value)
(                  # followed by a group that contains:
  [^.]*            #   anything but a dot, any number of times
  \.               #   a dot
  [^.]*            #   anything but a dot, any number of times
)                  # this is where the group closes; it it used to capture its content
$                  # the end of the string (nothing else is allowed after the group)

The replace string (\1) contains a back reference to that part of the input string that matches the first group from the regexp. The one and only group in the regexp matches the last two components of the domain, joined by the dot.

Remark

If the input string does not contain any dot then it has only one component. In this case the regexp doesn't match and preg_replace() returns the input string unaltered (which is what we also expect from it in this situation).

Upvotes: 0

axiac
axiac

Reputation: 72177

There are probably a handful of different solutions.

One of the smallest:

function domain($input)
{
    // Split the input string into pieces, use dot (.) as delimiter
    $pieces = explode('.', $input);
    // Get the last two pieces
    $domain = array_slice($pieces, -2);
    // Join them back using dot as delimiter and return
    return implode('.', $domain);
}

echo(domain('www.dev.community.google.com')."\n");
echo(domain('www.community.google.com')."\n");
echo(domain('community.google.com')."\n");
echo(domain('google.com')."\n");
echo(domain('com')."\n");

The output is:

google.com
google.com
google.com
google.com
com

Upvotes: 1

Samir Selia
Samir Selia

Reputation: 7065

Following solution will work for all kinds of url:

<?php

function getDomain($url)
{
    $exp = explode('.', $url);
    $count = count($exp);
    $tmp_arr[0] = $exp[$count-2];
    $tmp_arr[1] = $exp[$count-1];
    $final_str = implode('.', $tmp_arr);
    return $final_str;
}

$url = 'www.dev.community.google.com';
echo getDomain($url);

echo '<br/>';

$url = 'www.community.com';
echo getDomain($url);
?>

Output

google.com

community.com

Here is the working demo link.

Upvotes: 0

Raman Sahasi
Raman Sahasi

Reputation: 31841

just use addhttp() and get_domain() functions and use them like this:

<?php

print get_domain("www.dev.community.google.com");

function get_domain($url)
{
  $url = addhttp($url);
  $pieces = parse_url($url);
  $domain = isset($pieces['host']) ? $pieces['host'] : '';
  if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
    return $regs['domain'];
  }
  return false;
}

function addhttp($url) {
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}

Output

google.com

Upvotes: 0

Related Questions