Checking for multiple strpos values

后端 未结 7 1923
陌清茗
陌清茗 2021-01-05 05:51

I am wondering how to complete multiple strpos checks.

Let me clarify:
I want strpos to check the variable \"COLOR\" to see if any numbers fro

相关标签:
7条回答
  • 2021-01-05 06:38

    If all value is seperated by a space in value then you can do the following. Otherwise ignore it.

    It is needed because if you have $color="25"; then strpos will found both 2, 5 and 25 so required result will not come

    <?php
    $color='1 25 48 9 3';
    $color_array = explode(" ",$color);
    
    $find = range(1,8);//array containing 1 to 8
    
    $isFound = false;
    foreach($find as $value) {
        if(in_array($value, $color_array)) 
        {
            $isFound = true;
            break;
        }
    }
    
    if($isFound) {
        echo "Selected";
    }
    ?>
    
    0 讨论(0)
  • 2021-01-05 06:38
    if (preg_match('/string1|string2|string3/i', $str)){
      //if one of them found
    }else{
     //all of them can not found
    }
    
    0 讨论(0)
  • 2021-01-05 06:39

    try preg match for multiple

    if (preg_match('/word|word2/i', $str))
    

    strpos() with multiple needles?

    0 讨论(0)
  • 2021-01-05 06:42

    I just used the OR statement (||)

    <?php 
      if (strpos($color,'1') || strpos($color,'2') || strpos($color,'3') || strpos($color,'4') || strpos($color,'5') || strpos($color,'6') || strpos($color,'7') || strpos($color,'8') === true) 
       {
        //do nothing
       } else { 
                echo "checked"; 
              } 
    ?>
    
    0 讨论(0)
  • 2021-01-05 06:48

    A simple preg_match() call using wordboundaries around a numeric character class will be completely accurate and suitable for your task.

    The word boundary metacharacters ensure that full-integer matching is executed -- no false positive (partial) matching occurs.

    Code: (Demo)

    $array = array(
        'text 1 2 and 3 text',
        'text 3 9 25 text',
        'text 9 25 48 text',
    );
    
    foreach ($array as $color) {
        echo "\n---\n$color";
        echo "\n\t" , preg_match('~\b[1-8]\b~', $color, $out) ? "checked (satisfied by {$out[0]})" : 'not found';
        echo "\n\tChad says: " , (strpos($color,'1') || strpos($color,'2') || strpos($color,'3') || strpos($color,'4') || strpos($color,'5') || strpos($color,'6') || strpos($color,'7') || strpos($color,'8') ? 'found' : 'not found');
    }
    

    Output:

    ---
    text 1 2 and 3 text
        checked (satisfied by 1)
        Chad says: found
    ---
    text 3 9 25 text
        checked (satisfied by 3)
        Chad says: found
    ---
    text 9 25 48 text
        not found
        Chad says: found
    

    As for how to implement this technique in your script...

    if (!preg_match('~\b[1-8]\b~', $color)) {
        echo 'checked';
    }
    
    0 讨论(0)
  • 2021-01-05 06:49

    I had similar needs so here is function that get position of closest substring in given string, where search substrings are provided in array. It also pass by reference matched substring. Note that order matters in case some substring contains other substring - example: '...' and '.'.

    function strpos_arr($haystack, $needleN, $offset = 0, &$needle = '') {
      if (!is_array($needleN)) {
        return strpos($haystack, $needleN, $offset);
      } else {
        $res = FALSE;
        foreach ($needleN as $ind => $item) {
          $pos = strpos($haystack, $item, $offset);
          if ($pos !== FALSE && ($res === FALSE || $pos < $res)) {
            $res = $pos;
            $needle = $item;
          }
        }
        return $res;
      }
    }
    
    0 讨论(0)
提交回复
热议问题