在 PHP 中,您可以使用內置的字符串函數 strpos() 來檢查字符串中是否存在指定的字符串。
下面是使用 strpos() 函數檢測指定字符串是否存在的示例代碼:
在這個例子中,strpos() 函數接受兩個參數,第一個是要檢查的字符串,第二個是要查找的字符串。如果指定的字符串找到,則返回第一次出現的位置索引。如果未找到指定字符串,則返回 false。因此,我們使用了 !== 運算符來確保返回的位置索引不等於 false。
如果 $findString 存在於 $myString 中,那麼將會輸出 "The string 'sample' was found in the string 'This is a sample string'."。否則,將會輸出 "The string 'sample' was not found in the string 'This is a sample string'."。