【问题标题】:Checking for multiple strpos values检查多个 strpos 值
【发布时间】:2013-10-06 05:25:44
【问题描述】:

我想知道如何完成多个 strpos 检查。

让我澄清一下:
我希望 strpos 检查变量“COLOR”以查看变量中是否有从 1 到 8 的任何数字。如果存在从 1 到 8 的任何数字,我想回显“已选择”。

示例:
假设变量中只有数字 1,它回显“已选择”。
假设数字 1 2 和 3 在变量中,它回显“已选择”。
假设数字 3 9 25 在变量中,它回显“选择”(因为那个 3!!)。
假设变量中只有数字 9,它不会回显。
假设数字 9 25 48 在变量中,它不会回显。

【问题讨论】:

  • 你得到数组中的数字了吗?
  • 这个变量的字符串值中是否有空格,你想在其中查找。作为1 2 25 48
  • 数字之间有空格,但我想出了怎么做,下面的代码有效!谢谢。

标签: php strpos


【解决方案1】:

我只是使用了 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"; 
          } 
?>

【讨论】:

  • 如果$color='25 48'; 会输出什么? strpos($color,'5') 将找到数字 5,但您的值是 25 和 48。
  • 这是唯一适用于小值的代码。如果您有更大的值,我们可以使用一个函数将每个值与您拥有的模式进行比较。
  • strpos() 返回针在字符串中存在的位置,因此如果这些数字中的任何一个位于第一个位置,它将返回 0 并且您的代码将失败,因为您正在做布尔检查。此外,strpos() 永远不会返回布尔值 TRUE,因此检查 === true 是不正确的。
  • 但是检查 !== false 就可以了。
  • 您的代码仍然存在错误。只有 8 个不会出错。将其更改为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),它应该可以正常工作。 ps,我刚刚添加了一对额外的()
【解决方案2】:

尝试多个预匹配

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

strpos() with multiple needles?

【讨论】:

  • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post。 - From Review
  • @LucaDetomi 你确定吗?
  • 这是一条自动评论,因为您的回答被标记为太短。我建议您添加更多细节,让用户了解“为什么”您的解决方案是正确的,也许这可能是最好的解决方案。
  • 这实际上是错误的答案,可能是因为复制/粘贴。 /i 等效于 stripos() 对于 strpos 需要删除的 i
【解决方案3】:

我发现上面的答案不完整,并想出了我自己的功能:

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

用法:

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

【讨论】:

  • 您的功能和使用效果非常好。当我在我的应用程序中实现这一点时,我将strpos 更改为stripos,以便该函数不区分大小写。
【解决方案4】:

如果所有值都用空格分隔,那么您可以执行以下操作。 否则忽略它。

这是必需的,因为如果您有 $color="25";,那么 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";
}
?>

【讨论】:

  • 萨利姆,很好的收获。不过,我不需要执行此操作,因为我不会输入任何其他数字(1 到 8 除外)。相反,我将输入文本(例如颜色 BROWN 或 DARK GREY)。不过谢谢! +代表
【解决方案5】:
if (preg_match('/string1|string2|string3/i', $str)){
  //if one of them found
}else{
 //all of them can not found
}

【讨论】:

    【解决方案6】:

    在数字字符类周围使用单词边界的简单preg_match() 调用将完全准确并适合您的任务。

    单词边界元字符确保执行全整数匹配——不会发生误报(部分)匹配。

    代码:(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');
    }
    

    输出:

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

    至于如何在你的脚本中实现这种技术……

    if (!preg_match('~\b[1-8]\b~', $color)) {
        echo 'checked';
    }
    

    【讨论】:

      【解决方案7】:

      我有类似的需求,所以这里是获取给定字符串中最近子字符串位置的函数,其中搜索子字符串在数组中提供。它还通过引用匹配子字符串传递。 请注意,如果某些子字符串包含其他子字符串,则顺序很重要 - 例如:'...' 和 '.'。

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

      【讨论】:

      • 另请注意,如果第二个参数不是数组,则此函数作为 strpos 工作。
      猜你喜欢
      • 1970-01-01
      • 2011-10-17
      • 2013-11-12
      • 2021-08-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2013-10-14
      相关资源
      最近更新 更多