if(preg_match(/(www|co.uk|uk.com|com|net|edu|org|org.uk|info|me|biz|co|io)/, $hostParts)) {
//unset this element from the array;
}
我正在使用正则表达式删除常见的域扩展名。我的问题是我的表达式在www.cnet.com的示例中也与cnet中的网络匹配。我该如何停止与单词部分匹配的域名,例如www.cnet.com等,为此,我只想删除www和com部分。谢谢。
通常,\b
可以满足您的需求。用两个\b
s表示您的单词。but check the updates for your specific case
。
if(preg_match('/\b(www|co.uk|uk.com|com|net|edu|org|org.uk|info|me|biz|co|io)\b/si', $hostParts)) {
//unset this element from the array;
}
更新
这是一个更新,它特定于URL,我没有考虑到这一点:
if(preg_match('/(\bwww\.|(\.(co\.uk|uk\.com|com|net|edu|org|org.uk|info|me|biz|co|io)\b))/si', $hostParts)) {
//unset this element from the array;
}
更新2
这是一个示例,请注意,我已经将“ org”与“ org.uk”交换了,因为如果抓住了第一个,则第二个就不会了:
<?php
$str = 'www.cnet.org.uk';
$str = preg_replace('/(\bwww\.|(\.(co\.uk|uk\.com|com|net|edu|org.uk|org|info|me|biz|co|io)\b))/si', '', $str);
echo $str;
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句