我需要使用PHP打印出包含给定子字符串“ Happy”的所有字符串。$t包含所有输入数据。我尝试这样做,但是无法达到预期的输出。$i = 1;while ($t = dbNext($r)) { $temp = strtolower($t[0]); if(strpos($temp, 'happy')){ echo "$i - $t[0]"; echo "\n"; $i++; }}输入:1. Happy Gilmore2. The Fast and the Furious3. Happy, Texas4. The Karate Kid5. The Pursuit of Happyness6. Avengers: End Game7. Happy Feet8. Another Happy Day预期产量:1. Happy Gilmore2. Happy, Texas3. The Pursuit of Happyness4. Happy Feet5. Another Happy Day我得到的输出:1. The Pursuit of Happyness2. Another Happy Day
3 回答
哔哔one
TA贡献1854条经验 获得超8个赞
在PHP中,strpos在匹配时返回子字符串的起始索引,否则返回False。因此,当“ Happy”从索引0开始时,strpos返回0,从而使if校验为false。做这样的事情:
$i = 1;
while ($t = dbNext($r)) {
$temp = strtolower($t[0]);
$res = strpos($temp, 'happy');
if(gettype($res) == "integer" && $res >= 0){
echo "$i - $t[0]";
echo "\n";
$i++;
}
}
- 3 回答
- 0 关注
- 189 浏览
添加回答
举报
0/150
提交
取消