php - strpos 不适用于逗号分隔字符串中的第一个字符串
问题描述
看这个演示
我有两个逗号分隔的字符串列表,并且想要查找字符串并返回一条消息。我注意到,在我查找第一个列表的第一个字符串的特殊情况下,它不会找到它。如果我将该字符串移动到另一个位置,它会。无法理解为什么。
$dynamic_list = "AZ, CA, CO";
$static_list = "MN,WA,IA";
$search = "AZ";
if ( strpos($dynamic_list . ',' . $static_list, $search) == false && !empty($search) ) { // check string is not empty + that it is not on any of the lists
echo 'not found: String '.$search.' was not found in lists';
} else {
echo 'found';
}
解决方案
$dynamic_list = "AZ, CA, CO";
$static_list = "MN,WA,IA";
$search = "AZ";
if ( strpos($dynamic_list . ',' . $static_list, $search) === false && !empty($search) ) { // check string is not empty + that it is not on any of the lists
echo 'not found: String '.$search.' was not found in lists';
} else {
echo 'found';
}
添加 === 然后尝试
推荐阅读
- python - 使用 Python 进行 DNA 到 RNA 转换:如何在不替换/更改预先替换的输入的情况下替换文本输入?
- sql - 从一个范围内每年提取价值
- python - 如何从 azure ml 服务数据集中访问文件或文件夹?
- conditional-statements - TYPO3 10.4 LTS 中的嵌套数组条件
- python - 过滤低于阈值的连续的
- javascript - Discords.js 向服务器中的每个通道发送消息
- excel - 从第一个到最后一个连接,但在另一列中连接
- bluetooth-lowenergy - 如何在 Linux 内核 3.10.0 版本中启用或反向移植蓝牙 BLE 功能?
- python - 在实感中共享上下文时进程冻结
- python - 如何使用 rpython 翻译器将我的 python 脚本转换为 pypy?