要檢測一個字符串中是否包含子字符串,可以使用PHP中的strpos()函數。該函數可以在一個字符串中查找另一個字符串第一次出現的位置,并返回其位置的索引。如果找不到子字符串,則返回false。
以下是一個示例代碼,演示如何使用strpos()函數來檢測一個字符串中是否包含子字符串:
$string = "Hello, world!";
$substring = "world";
if(strpos($string, $substring) !== false){
echo "String contains substring.";
} else {
echo "String does not contain substring.";
}
在上面的示例中,如果字符串 “Hello, world!” 包含子字符串 “world”,則輸出 “String contains substring.”;如果不包含,則輸出 “String does not contain substring.”。
請注意,strpos()函數區分大小寫。如果要進行不區分大小寫的檢測,可以使用stripos()函數。