gpt4 book ai didi

php - 搜索一个字符串是否存在于另一个字符串中

转载 作者:可可西里 更新时间:2023-11-01 13:35:00 25 4
gpt4 key购买 nike

为了我的目的,我这样做了:

<?php
$mystring = 'Gazole,';
$findme = 'Sans Plomb 95';
$pos = strpos($mystring, $findme);

if ($pos >= 0) {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
} else {
echo "The string '$findme' was not found in the string '$mystring'";
}
?>

但是,它总是执行这个分支:

echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";

虽然我要搜索的字符串不存在。

请提前帮忙,thx :))

最佳答案

正确的做法是:

if ($pos !== false) {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
} else {
echo "The string '$findme' was not found in the string '$mystring'";
}

参见 giant red warning在文档中。

关于php - 搜索一个字符串是否存在于另一个字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5777617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com