在字符串中查找 TOP 或 BRITISH
<?php
$var1 = "Top of British";
$var2 = "Welcome to British, the TOP country in the world";
$var1 = strtolower($var1);
$var2 = strtolower($var2);
if (strpos($var1, 'top') && strpos($var1, 'british')) {
echo "Either the word TOP or the word BRITISH was found in string 1";
}
?>
更普遍地将字符串 2 中的单词与字符串 1 中的单词匹配
<?php
$var1 = explode(' ', strtolower("Top of British"));
$var2 = "Welcome to British, the TOP country in the world";
$var2 = strtolower($var2);
foreach($var1 as $needle) if (strpos($var2, $needle)) echo "At least one word in str1 was found in str 2";
?>