如何检查子字符串是否存在于主字符串中


How to check the sub string is exists or not in main string

是否有办法检查子字符串是否存在于主字符串中?如果退出,则不需要任何其他操作。如果不存在,我想添加另一个字符串到主字符串。

例如:

$a = 'Hello world';
$b = 'Hello';

我想检查$b是否存在于$a中。如果存在,我将该变量发送到数据库。如果不存在,我想使用str_replace('Hello', $b.'World', $b)

if (strpos($a, $b) !== false) {
    // $b is in $a
} else {
    // $b is not in $a
    str_replace('Hello', $b . 'World', $b);
}

strpos() .

如果没有找到子字符串,则返回FALSE

您正在寻找strpos函数

请记住使用===而不是==