你的代碼是正確的,做工精細,像這樣的例子腳本和輸出:
<?php
$contents = [
'go to sleep',
'I will drink water',
'this is a beep',
'[email protected]',
'google.com',
'endswithcom',
'this doesn\'t match'
];
foreach ($contents as $content) {
print "content \"$content\" ";
if (preg_match("/(sleep|drink|beep|@|\\.com)/i", $content)) {
print " match\n";
} else {
print " doesn't match\n";
}
}
輸出:
content "go to sleep" match
content "I will drink water" match
content "this is a beep" match
content "[email protected]" match
content "google.com" match
content "endswithcom" doesn't match
content "this doesn't match" doesn't match
像@ Rizier123解釋,我用的\\.com
不是僅僅.com
因爲。 (點)是一個特殊的字符,這意味着任何字符,在我們的例子,如果我們不逃避點字符內容endswithcom
將匹配。 (見PCRE regex dot syntax)
'/'='|!'這是不一樣的性格 – Rizier123
逃亡'.'它是一個特殊字符。 – Rizier123