【发布时间】:2017-09-27 08:54:47
【问题描述】:
我需要比较 URL 并从数组中删除重复项,但我只想比较 url 中的主机。当我比较时,我需要跳过 http 和 https 和 www 以及最后一个斜杠。 所以当我有数组时:
$urls = array(
'http://www.google.com/test',
'https://www.google.com/test',
'https://www.google.com/example',
'https://www.facebook.com/example',
'http://www.facebook.com/example');
结果将只有
http://www.google.com/test
http://www.google.com/example
http://www.facebook.com/example
我试着比较一下:
$urls = array_udiff($urls, $urls, function ($a, $b) {
return strcmp(preg_replace('|^https?://(www\\.)?|', '', rtrim($a,'/')), preg_replace('|^https?://(www\\.)?|', '', rtrim($b,'/')));
});
但它返回给我一个空数组。
【问题讨论】:
-
也许添加正则表达式标签。
-
看看this
-
但是你在哪里可以给我展示工作示例或任何想法?
-
我需要比较没有 www 并且需要选择数组
标签: php arrays unique distinct