douzhou7037
douzhou7037
2019-07-03 12:06

检查字符串是否存在于两个字符串列表中(以逗号分隔)

已采纳

I want to check if a given 2-character value (my input: $string), exists inside any of my two separate lists, of possible matching values:

One list is dynamic (it will be read from my DB), and the values in it are separated by commas, with space after each comma:

It can be for example:

$dynamic_list="AA, AB, BA"
$dynamic_list="BC"
$dynamic_list="" (Empty)

The second list is a static list that I will maintain myself, and should be within the script itself.

I’m not sure how to build it. Maybe just define a variable like:

$static_list="MC,JL,EO";

If the string matches any of the strings in any of those lists, I just want to output that string, otherwise output "no match".

What is the best and fast way to do this?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • douguanyan9928 douguanyan9928 2年前

    Concatenate the two strings with the same delimiter that the strings use, and check as normal.

    Performing aditional string or array manipulations are needless overhead.

    Code: (Demo)

    $dynamic_list = "AA, AB, BA";
    $static_list = "MC,JL,EO";
    
    $search = "JL";
    
    if (strpos($dynamic_list . ',' . $static_list, $search) !== false) {
        echo 'found';
    } else {
        echo 'not found';
    }
    

    But, really, you should check the static string first. Then only bother making a trip to the database IF you don't have a match in the static string.

    As for querying, you might count the number of rows where the search string is found by LOCATE() -- since you don't need the data, just confirmation of a match.

    点赞 评论 复制链接分享
  • dtbam62840 dtbam62840 2年前

    use in_array to search inside array

    $dynamic_list="AA, AB, BA";
    $dynamic_list_array=explode(", ", $dynamic_list);
    
    $static_list="MC,JL,EO";
    $static_list_array=explode(",", $static_list);
    
    $tofind="DD";
    
    if(in_array($tofind, $dynamic_list_array) || in_array($tofind, $static_list_array))
        echo "found";
    else
        echo"not found";
    
    点赞 评论 复制链接分享
  • doudu9094 doudu9094 2年前

    A simple way of achieving this is rather than split it into an array and checking is to first ensure that the string ends with the same character as the separator and the do a search in the string for the required item with the separator. To improve the accuracy of the search, it is also better to prefix the string with the separator and the same with the string to search for.

    So in the first example, AA, AB, BA will be made into , AA, AB, BA, and then a search is done (in this example) for , BA,...

    $dynamic_list="AA, AB, BA";
    $static_list="MC,JL,EO";
    $to_find = "BA";
    
    if ( strpos(", ".$dynamic_list.", ", ", ".$to_find.", ") !== false
            || strpos(",".$static_list.",", ",".$to_find.",") !== false)    {
        echo $to_find.PHP_EOL;    
    }
    else    {
        echo "Not found";
    }
    

    As the two strings have different separators it would be slight differences, but that should be easy enough if you use the same separator as you can combine it into 1 search...

    $dynamic_list="AA, AB, BA";
    $static_list="MC, JL, EO";
    $to_find = "BA";
    $separator = ", ";
    if ( strpos($separator.$dynamic_list.$separator.$static_list.$separator
            , $separator.$to_find.$separator) !== false)    {
        echo $to_find.PHP_EOL;    
    }
    else    {
        echo "Not found";
    }
    
    点赞 评论 复制链接分享

相关推荐