PHP 检查字符串中的子串
要检查字符串中的子串,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
广告