在PHP中减少字符串

在PHP中减少字符串,php,Php,我正在处理一件非常简单的事情,但我发现要解决这个问题非常冗长 我有一个字符串,它有一个由用户管理的网址。。。因此,它可以是: foodoman.com 这个名单还能继续下去吗 是否有一种简单的方法可以识别并将其剪切到mydomain.com?这样才能保持统一 function after ($this, $inthat) { if (!is_bool(strpos($inthat, $this))) return substr($inthat, strpos($inthat,$this)

我正在处理一件非常简单的事情,但我发现要解决这个问题非常冗长

我有一个字符串,它有一个由用户管理的网址。。。因此,它可以是:

  • foodoman.com
  • 这个名单还能继续下去吗
是否有一种简单的方法可以识别并将其剪切到mydomain.com?这样才能保持统一

function after ($this, $inthat) {
if (!is_bool(strpos($inthat, $this)))
return substr($inthat, strpos($inthat,$this)+strlen($this));
};
$web_data="";
if(!strpos($data["web"], "http://") {
$web_data=after ("http:\\", $data["concert_web"]);
}
if(!strpos($data["web"], "https://") {
$web_data=after ("https://", $data["concert_web"]);
}
if(!strpos($data["web"], "www") {
$web_data=after ("www", $data["concert_web"]);
}

我收集了剧本,但感觉不对,这是未来的证明吗?我渴望了解并提前感谢您的任何评论。

您可以使用前面提到的和RegEx(via)的组合



$DomainOnly
将是
domain.tld
。所有子域都将被删除。

您可以使用前面提到的和RegEx(via)的组合



$DomainOnly
将是
domain.tld
。所有子域都将被删除。

我个人会将regex与
preg\u replace
结合使用。大概是这样的:

// Strings for testing the case.
$domains = [
  'foodomain.com',
  'www.foodomain.com',
  'http://foodomain.com',
  'https://www.foodomain.com',
  'https://www.foodomain.www.com',
];


$result_strings = preg_replace(
  [
    '#^https?://#', // First, remove protocol.
    '#^www.#',      // Remove www. from the beginning.
  ],
  '', // Replace by an empty string.
  $domains // You can pass your string here.
);

print_r($result_strings);  // It will output result string for each of the domains from $domains array.
输出:

Array
(
    [0] => foodomain.com
    [1] => foodomain.com
    [2] => foodomain.com
    [3] => foodomain.com
    [4] => foodomain.www.com
)

我个人会将regex与
preg\u replace
结合使用。大概是这样的:

// Strings for testing the case.
$domains = [
  'foodomain.com',
  'www.foodomain.com',
  'http://foodomain.com',
  'https://www.foodomain.com',
  'https://www.foodomain.www.com',
];


$result_strings = preg_replace(
  [
    '#^https?://#', // First, remove protocol.
    '#^www.#',      // Remove www. from the beginning.
  ],
  '', // Replace by an empty string.
  $domains // You can pass your string here.
);

print_r($result_strings);  // It will output result string for each of the domains from $domains array.
输出:

Array
(
    [0] => foodomain.com
    [1] => foodomain.com
    [2] => foodomain.com
    [3] => foodomain.com
    [4] => foodomain.www.com
)

您可以使问题标题更清晰,并且在考虑正确的标题时可能会找到以下链接:。但是,请注意www.domain.com与domain.com
$host=parse_url($string,PHP_url_host)不同可能?是的
parse_url()
是您正在寻找的。如果你不想在那里看到它,你仍然需要去掉
www.
,但这是一个好的开始,非常感谢你所有的建议和良好实践。我正在跟进,以了解更多信息。再次感谢。我想我不确定我要找的是什么,所以请为标题不好道歉。您可以让您的问题标题更清楚,并且在考虑正确的标题时可能会找到以下链接:。但是,请注意www.domain.com与domain.com
$host=parse_url($string,PHP_url_host)不同可能?是的
parse_url()
是您正在寻找的。如果你不想在那里看到它,你仍然需要去掉
www.
,但这是一个好的开始,非常感谢你所有的建议和良好实践。我正在跟进,以了解更多信息。再次感谢你。我想我不知道我在找什么,所以为这个标题太弱而道歉,或者一次做所有的
$result\u strings=preg\u replace(“#^https?:/(www\)?,“,$domains”)@revo不包括“www.foodomain.com”案例。你不能包括它<代码>$result_strings=preg_replace(“#^(https?:/)?(www\)?#”和“,$domains)谢谢我使用了revo的示例来进行简短的编码,但是非常感谢@kalabro的详细解释,它非常有用。非常感谢大家在这件事上的帮助。或者一次完成所有的
$result\u strings=preg\u replace(“^https?”:/(www\)?”,“$domains”)@revo不包括“www.foodomain.com”案例。你不能包括它<代码>$result_strings=preg_replace(“#^(https?:/)?(www\)?#”和“,$domains)谢谢我使用了revo的示例来进行简短的编码,但是非常感谢@kalabro的详细解释,它非常有用。非常感谢大家在这件事上的帮助。