如何删除字符串中倒数第二个点之前的所有内容

时间:2016-01-26 08:48:09

标签: php

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

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

所以关键是要删除之前的所有内容,包括倒数第二个点。我怎么能用PHP做到这一点?

4 个答案:

答案 0 :(得分:1)

可能有一些不同的解决方案。

最小的一个:

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

答案 1 :(得分:0)

以下解决方案适用于各种网址:

          var headersAreFixed = $q.defer();

          function fixHeaderWidths() {
            if (!$element.find("thead th .th-inner").length) {
              $element.find("thead th").wrapInner('<div class="th-inner"></div>');
            }
            if($element.find("thead th .th-inner:not(:has(.box))").length) {
              $element.find("thead th .th-inner:not(:has(.box))").wrapInner('<div class="box"></div>');
            }

            $element.find("table th .th-inner:visible").each(function (index, el) {
              el = angular.element(el);
              var width = el.parent().width(),
                lastCol = $element.find("table th:visible:last"),
                headerWidth = width;
              if (lastCol.css("text-align") !== "center") {
                var hasScrollbar = $element.find(".scrollArea").height() < $element.find("table").height();
                if (lastCol[0] == el.parent()[0] && hasScrollbar) {
                  headerWidth += $element.find(".scrollArea").width() - $element.find("tbody tr").width();
                  headerWidth = Math.max(headerWidth, width);
                }
              }
              var minWidth = _getScale(el.parent().css('min-width')),
                title = el.parent().attr("title");
              headerWidth = Math.max(minWidth, headerWidth);
              el.css("width", headerWidth);
              if (!title) {
                // ordinary column(not sortableHeader) has box child div element that contained title string.
                title = el.find(".title .ng-scope").html() || el.find(".box").html();
              }
              //el.attr("title", title.trim());
            });
            headersAreFixed.resolve();
          }
  

输出

     

google.com

     

community.com

以下是工作演示link

答案 2 :(得分:0)

使用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)包含back reference到输入字符串中与regexp中的第一个组匹配的部分。 regexp中唯一的一个组与域的最后两个组件匹配,由点连接。

<强>备注

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

答案 3 :(得分:-1)

只需使用addhttp()get_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;
}

<强>输出

google.com
相关问题