这是我迄今为止的代码。如何从php/regex中删除域名中的TLD
$main = 'example.com';
$removed = str_ireplace("cant figure out what to put in here...", " ", "$main");
echo $removed;`
我知道我可以只是把.COM第一paramerter但也有数百种不同的TLD的IM试图删除。
这是我迄今为止的代码。如何从php/regex中删除域名中的TLD
$main = 'example.com';
$removed = str_ireplace("cant figure out what to put in here...", " ", "$main");
echo $removed;`
我知道我可以只是把.COM第一paramerter但也有数百种不同的TLD的IM试图删除。
使用parse_url可以删除URL,并且http-build-url可以在删除主机后进行备份。
一个简单的例子(警告 - 可能行不通,之后我会解释为什么)。
$url = "http://example.com:port/path?query=parts&so=on";
$aParts = parse_url($url);
$aParts['host'] = '';
$removed = http_build_url($aParts);
echo $removed;
但是这可能不是原因有二:
因此,一个更好的解决办法是:
$url = "http://example.com:port/path?query=parts&so=on";
$aParts = parse_url($url);
$aParts['host'] = '';
$removed = ($aParts['host'] ? $aParts['host'] : 'http') . '://'
'' . // This is where the host goes
($aParts['post'] ? ':' . $aParts['port'] : '') .
($aParts['path'] ? '/' . $aParts['path'] : '') .
($aParts['query'] ? '?' . $aParts['query'] : '') .
($aParts['fragment'] ? '#' . $aParts['fragment'] : '');
(未测试 - 道歉对任何错误,但你shouldge的想法)
添加的用户,并通过必要时(检查文档上面链接),如果你想更换主机不留空白 - 你看看在哪里做。
这就是我想出的感谢robby指引我在正确的方向。
$ url ='example.com.uk';
$ removed = stristr($ url,'。',true);
echo“$ removed”;
只是问......为什么要从URL中删除TLD ...? –
请看http://php.net/manual/en/function.preg-replace.php – Serge
Bergi,我不是很擅长php,但我已经尝试过.com | .org等并不真的想这样做,因为有数百个。但它无功无用。我一整晚都在努力。弗雷德里克,我想删除它,所以我可以统计域名中的字符减去tld –