Php 如何删除字符串中最后一个点之前的所有内容

Php 如何删除字符串中最后一个点之前的所有内容,php,Php,我正在尝试制作一个函数,它可以删除倒数第二个点之前的所有内容。如果字符串类似于www.dev.community.google.com,那么我想删除google.com之前的所有内容 这些子域并不总是相同的,有时它们只是www.google.com,有时是www.community.google.com 因此,关键是要删除之前的所有内容,包括从第二个点到最后一个点。如何使用php实现这一点?只需使用addhttp()和get\u domain()函数,并像这样使用它们: <?php pr

我正在尝试制作一个函数,它可以删除倒数第二个点之前的所有内容。如果字符串类似于
www.dev.community.google.com
,那么我想删除
google.com
之前的所有内容

这些子域并不总是相同的,有时它们只是
www.google.com
,有时是
www.community.google.com

因此,关键是要删除之前的所有内容,包括从第二个点到最后一个点。如何使用php实现这一点?

只需使用
addhttp()
get\u domain()
函数,并像这样使用它们:

<?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;
}

以下解决方案适用于所有类型的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);
?>

输出

谷歌网站

社区网


这是正在运行的演示。

可能有几种不同的解决方案

最小的一个:

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");
输出为:

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

另一种解决方案,使用
regexp
识别并仅保留最后两个组件:

function domain($input)
{
    return preg_replace('/^.*\.([^.]*\.[^.]*)$/', '\1', $input);
}
正则表达式没有什么特别之处:

^                  # 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)
替换字符串(
\1
)包含到输入字符串中与
regexp
中的第一个组匹配的部分的。
regexp
中唯一的一个组匹配域的最后两个组件,并由点连接

备注


如果输入字符串不包含任何点,则它只有一个组件。在这种情况下,
regexp
不匹配,并返回未更改的输入字符串(这也是我们在这种情况下所期望的)。

查看我的帖子。它适用于所有类型的URL。改进语法和prasingNp。创建了可重用的功能;)
^                  # 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)