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

更新日期:24-12-2019

186 次浏览

启动你的 职业

完成课程获得认证

开始
广告