简体   繁体   English

检查多个 strpos 值

[英]Checking for multiple strpos values

I am wondering how to complete multiple strpos checks.我想知道如何完成多个 strpos 检查。

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.我希望 strpos 检查变量“COLOR”以查看变量中是否有从 1 到 8 的任何数字。 If any numbers from 1 to 8 are present, I want to echo "selected".如果存在从 1 到 8 的任何数字,我想回显“已选择”。

Examples:例子:
Let's say only the number 1 is in the variable, it will echo "selected".假设变量中只有数字 1,它将回显“已选择”。
Let's say the numbers 1 2 and 3 are in the variable, it will echo "selected."假设数字 1 2 和 3 在变量中,它将回显“已选择”。
Let's say the numbers 3 9 25 are in the variable, it will echo "selected" (because of that 3.!).假设数字 3 9 25 在变量中,它将回显“selected”(因为 3.!)。
Let's say only the number 9 is in the variable, it will NOT echo.假设变量中只有数字 9,它不会回显。
Let's say the numbers 9 25 48 are in the variable, it will NOT echo.假设数字 9 25 48 在变量中,它不会回显。

I just used the OR statement (||)我刚刚使用了 OR 语句 (||)

<?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"; 
          } 
?>

try preg match for multiple尝试预匹配多个

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

strpos() with multiple needles? strpos() 多针?

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>";         
    }       
  }

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";这是必要的,因为如果你有$color="25"; then strpos will found both 2, 5 and 25 so required result will not come然后strpos将同时找到 2、5 和 25,因此不会出现所需的结果

<?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";
}
?>
if (preg_match('/string1|string2|string3/i', $str)){
  //if one of them found
}else{
 //all of them can not found
}

A simple preg_match() call using wordboundaries around a numeric character class will be completely accurate and suitable for your task.使用围绕数字字符类的字边界的简单preg_match()调用将完全准确并适合您的任务。

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

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

Please Try this, it will help you.请试试这个,它会帮助你。

if ( (strpos($str, $your_string) !== false) || (strpos($your_string, $str) !== false) ) {
    // Matched
} else {
    // Not Matched
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM