首页 > 解决方案 > 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';
}

标签: phpstrpos

解决方案


    $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';
    }

添加 === 然后尝试


推荐阅读