本篇文章将给大家介绍关于php中检查字符串是否包含子字符串的方法。例如,只有当任何输入字符串中包含另一个子字符串时,才能运行特定的代码行,下面我们来看具体的内容。
我们来看具体的示例
例1:
以下代码的值将为true,因为主字符串$str包含子字符串“this”。这将会输出“true”。
<?php$str = 'this is main string'; if (strpos($str, 'this') !== false) { echo 'true';}?>
例2:
以下代码的值将为false,因为主字符串$str不包含子字符串“hello”。就不会输出了。
<?php$str = 'this is main string';$substr = "hello"; if (strpos($str, $substr) !== false) { echo 'true';}?>
例3:字符串开始时包含子字符串
下面的代码将检查字符串是否在开始处与子字符串连接。以下代码的值将为true,因为主字符串$str在开头包含子字符串“this”。
<?php$str = 'this is main string'; if (strpos($str, 'this') === 0 ) { echo 'true';}?>
本篇文章到这里就已经全部结束了,更多精彩内容大家可以关注的其他相关栏目教程!!!
以上就是如何在php中检查字符串是否包含子字符串的详细内容。