PHP 检查字符串中的子字符串
phpserver side programmingprogramming更新于 2025/8/29 21:37:17
要检查字符串中的子字符串,PHP 代码如下 −
示例
<?php $subStr = "Mother"; $str = "How I Met Your Mother"; echo "String = $str"; echo "
Substring = $subStr"; if(strpos($str, $subStr) !== false){ echo "
Substring found successfully"; } else{ echo "
Substring not found"; } ?>
输出
这将产生以下输出−
String = How I Met Your Mother Substring = Mother Substring found successfully
示例
现在让我们看另一个例子 −
<?php $subStr = "Ocean"; $str = "In the Heart of Sea"; echo "String = $str"; echo "
Substring = $subStr"; if(strpos($str, $subStr) !== false){ echo "
Substring found successfully"; } else{ echo "
Substring not found"; } ?>
输出
这将产生以下输出−
String = In the Heart of Sea Substring = Ocean Substring not found

