Checking for multiple strpos values

2019-02-14 04:10发布

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 from 1 to 8 are anywhere in the variable. If any numbers from 1 to 8 are present, I want to echo "selected".

Examples:
Let's say only the number 1 is in the variable, it will echo "selected".
Let's say the numbers 1 2 and 3 are in the variable, it will echo "selected."
Let's say the numbers 3 9 25 are in the variable, it will echo "selected" (because of that 3!!).
Let's say only the number 9 is in the variable, it will NOT echo.
Let's say the numbers 9 25 48 are in the variable, it will NOT echo.

标签: php strpos
5条回答
SAY GOODBYE
2楼-- · 2019-02-14 04:43

try preg match for multiple

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

strpos() with multiple needles?

查看更多
男人必须洒脱
3楼-- · 2019-02-14 04:45
if (preg_match('/string1|string2|string3/i', $str)){
  //if one of them found
}else{
 //all of them can not found
}
查看更多
何必那么认真
4楼-- · 2019-02-14 04:48

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";
}
?>
查看更多
一纸荒年 Trace。
5楼-- · 2019-02-14 04:53

I found the above answers incomplete and came up with my own function:

/**
 * Multi string position detection. Returns the first position of $check found in 
 * $str or an associative array of all found positions if $getResults is enabled. 
 * 
 * Always returns boolean false if no matches are found.
 *
 * @param   string         $str         The string to search
 * @param   string|array   $check       String literal / array of strings to check 
 * @param   boolean        $getResults  Return associative array of positions?
 * @return  boolean|int|array           False if no matches, int|array otherwise
 */
function multi_strpos($string, $check, $getResults = false)
{
  $result = array();
  $check = (array) $check;

  foreach ($check as $s)
  {
    $pos = strpos($string, $s);

    if ($pos !== false)
    {
      if ($getResults)
      {
        $result[$s] = $pos;
      }
      else
      {
        return $pos;          
      }
    }
  }

  return empty($result) ? false : $result;
}

Usage:

$string  = "A dog walks down the street with a mouse";
$check   = 'dog';
$checks  = ['dog', 'cat', 'mouse'];

#
# Quick first position found with single/multiple check
#

  if (false !== $pos = multi_strpos($string, $check))
  {
    echo "$check was found at position $pos<br>";
  }

  if (false !== $pos = multi_strpos($string, $checks))
  {
    echo "A match was found at position $pos<br>";
  }

#
# Multiple position found check
#

  if (is_array($found = multi_strpos($string, $checks, true)))
  {
    foreach ($found as $s => $pos)
    {
      echo "$s was found at position $pos<br>";         
    }       
  }
查看更多
SAY GOODBYE
6楼-- · 2019-02-14 05:08

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"; 
          } 
?>
查看更多
登录 后发表回答