PHP中有两种方法可以判断一个字符串是否包含另一个字符串:使用strpos()函数:
PHP中有两种方法可以判断一个字符串是否包含另一个字符串:
1. 使用strpos()函数:
strpos()函数用于检查字符串在另一个字符串中的位置,如果找到,则返回字符串在另一个字符串中的位置,如果没有找到,则返回false。
代码示例:
$str = "Hello world!";
if (strpos($str, "world") !== false) {
echo "The string 'world' was found in the string '$str'";
} else {
echo "The string 'world' was not found in the string '$str'";
}
?>
2. 使用strstr()函数:
strstr()函数用于检查字符串在另一个字符串中是否存在,如果存在,则返回从该位置开始的字符串,如果不存在,则返回false。
代码示例:
$str = "Hello world!";
if (strstr($str, "world") !== false) {
echo "The string 'world' was found in the string '$str'";
} else {
echo "The string 'world' was not found in the string '$str'";
}
?>
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(15条)