如何使用php/regex从域名中删除TLD
这是我目前掌握的代码如何使用php/regex从域名中删除TLD,php,regex,Php,Regex,这是我目前掌握的代码 $main = 'example.com'; $removed = str_ireplace("cant figure out what to put in here...", " ", "$main"); echo $removed;` 我知道我可以将.com放在第一个参数中,但我正试图删除数百个不同的TLD。用于删除URL,并在删除主机后重新构建它 一个简单的例子(警告-可能不起作用,我会在后面解释原因)是 但这可能不起作用,原因有二: http_build_url
$main = 'example.com';
$removed = str_ireplace("cant figure out what to put in here...", " ", "$main");
echo $removed;`
我知道我可以将.com放在第一个参数中,但我正试图删除数百个不同的TLD。用于删除URL,并在删除主机后重新构建它
一个简单的例子(警告-可能不起作用,我会在后面解释原因)是
但这可能不起作用,原因有二:
$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'] : '');
(未经测试-为任何错误道歉,但你应该放弃这个想法)
如果需要,请添加用户并通过(检查上面链接的文档),如果您想更换主机,请不要留空-您可以查看在哪里执行此操作。这是我想到的,感谢robby为我指明了正确的方向
$url = 'example.com.uk';
$removed = stristr($url, '.',true);
echo "$removed";
只是问。。。为什么要从URL中删除TLD…?请看Bergi,我还不太擅长php,但我已经尝试过了.com |.org等等,我真的不想这样做,因为有数百个。但它无论如何都不起作用。我整晚都在做这个。Frederick,我想删除它,这样我就可以计算域中的字符数减去tldserge,我今晚已经读了100遍了:(谢谢你,如果有人进入bbc.co.uk会发生什么?如果答案是bbc,那么你需要使用一个大的查找表。否则,这只是一个strrpos/substr问题。在第一次出现句点之后,要删除任何内容似乎需要做很多工作。url总是不带http://no子目录。它总是exa。)我所要做的就是使用正则表达式代码删除第一个之后的任何内容。我开始认为正则表达式不能与str_ireplace一起使用,这可能就是我的问题所在。preg_replace或preg_match是正则表达式想要的。或者你只得到域,而不是url。在这种情况下,stristr()将为您创造奇迹。
$url = 'example.com.uk';
$removed = stristr($url, '.',true);
echo "$removed";