Checking for multiple strpos values

strpos multiple occurrences
strpos(stata)
stripos multiple needles
strpos sql
preg_match
php string contains
php strpos array
php search for multiple values in string

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.

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

php, I have following code which check does targeted word exits in sentence and it's working. [CODE] The STRPOS function is case-sensitive. Property Value/Return Value. Type: Integer. The position of SubString in String. Remarks. The STRPOS function returns the position of the first occurrence of the substring. If SubString cannot be found, then the function returns zero. If String or SubString is empty, then the function returns zero. Example

try preg match for multiple

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

strpos() with multiple needles?

strpos - Manual, 5, PHP 7). strpos — Find the position of the first occurrence of a substring in a string information. Use the === operator for testing the return value of this function. search for multiple needles at once and it wil return a position when one of Parameters. haystack. The string to search in. needle. If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. This behavior is deprecated as of PHP 7.3.0, and relying on it is highly discouraged.

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

stripos - Manual, Use the === operator for testing the return value of this function. case insensitive; strpos() - Find the position of the first occurrence of a substring in a string I found myself needing to find the first position of multiple needles in one haystack. Return Value: Returns the position of the first occurrence of a string inside another string, or FALSE if the string is not found. Note: String positions start at 0, and not 1. PHP Version: 4+ Changelog: PHP 7.1.0 - The start parameter can be a negative number

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

How to find multiple needles in strpos Solutions, Find answers to How to find multiple needles in strpos from the expert community at Experts Exchange. sorry not at a box I can test on right now. tmwsiy. Example of PostgreSQL STRPOS() function using column: Sample Table: employees If we want to display the employee_id, first_name, last_name and the position of a specific substring 'lia', which must exists within the column first_name from employees table, the following SQL can be used.

if (preg_match('/string1|string2|string3/i', $str)){
  //if one of them found
}else{
 //all of them can not found
}

php - Checking for multiple strpos values, So you need to check the return value of strpos using the === operator which check the value and the type rather than using == which just checks value. I did originally have it checking for truthy values instead of type checking, but it was recommended that I edit it to use the triple equals operator. – christopher Mar 3 '16 at 9:51 If performance is important, it's usually better to avoid using preg_match() for this kind of simple string search since it's fairly slow compared to other

Finding a string within a string: strpos() and stripos() – Hacking with , That will return 8, because the first character in "This is a strpos() test" that is a will check the value of 0 and false and find they match (both false), then check  You can use strpos () or stripos () to check if the string contain the given needle. It will return the position where it was found, otherwise will return FALSE. Use the operators === or `!== to differ FALSE from 0 in PHP.

PHP strpos() Function, Return Value: Returns the position of the first occurrence of a string inside another string, or FALSE if the string is not found. Note: String positions start at  Sometimes the answers to multiple responses are put into one string variable. Commonly, this is the concatenation of the codes of possible (positive) answers. For our example data, such a variable could look like the following (using numeric codes): spkg1 1. 16 2. 521 3. 5 4. 3 5. 63.

PHP, This returns an integer value of the position of the first occurrence of the string. This function is case-sensitive, which means that it treats upper-case and lower-  As you can see in the output, missing values are at the listed after the highest value 2.1. This is because Stata treats a missing value as the largest possible value (e.g., positive infinity) and that value is greater than 2.1, so then the values for newvar1 become 0. tab trial2, missing

Comments
  • are you getting numbers in array?
  • Is there any space in this string value of variable in which you want to find. as 1 2 25 48
  • There are spaces between the numbers, but I figured out how to do it, the code below works! Thank you.
  • If $color='25 48'; what will be the out put? strpos($color,'5') will found digit 5 but you have value 25 and 48.
  • This is the only code works better for small values . if you have bigger values us a function that compare each value to the pattern you have .
  • strpos() returns the position of where the needle exists in the string, so if any of those numbers are in the first position it would return 0 and your code would fail because you're doing a boolean check. Also, strpos() would never return a boolean TRUE so checking === true is incorrect.
  • but checking against !== false will do it.
  • your code is still bugged. only 8 will not bug out. change it to 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) and it should work correctly. ps, i just added an extra pair of ( )
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. - From Review
  • @LucaDetomi are you sure?
  • This is an automatic comment because your answer was flagged as too short. I suggest you to add more details to let users understand "why" your solution is correct andmaybe why this could be the best one.