PHP检查字符串中包含的子串
要检查字符串中的子字符串,代码在PHP中如下所示:
示例
<?php $subStr = "Mother"; $str = "How I Met Your Mother"; echo "String = $str"; echo "\nSubstring = $subStr"; if(strpos($str, $subStr) !== false){ echo "\n成功找到子字符串"; } else{ echo "\n找不到子字符串"; } ?>
输出结果
String = How I Met Your Mother Substring = Mother 成功找到子字符串
示例
现在让我们来看另一个示例-
<?php $subStr = "Ocean"; $str = "In the Heart of Sea"; echo "String = $str"; echo "\nSubstring = $subStr"; if(strpos($str, $subStr) !== false){ echo "\n成功找到子字符串"; } else{ echo "\n找不到子字符串"; } ?>
输出结果
String = In the Heart of Sea Substring = Ocean 找不到子字符串