Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for multiple strpos values

Tags:

php

strpos

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.

like image 929
Chad Cardiff Avatar asked Oct 06 '13 05:10

Chad Cardiff


People also ask

What is the strpos () function used for?

strpos in PHP is a built-in function. Its use is to find the first occurrence of a substring in a string or a string inside another string. The function returns an integer value which is the index of the first occurrence of the string.

What is strpos ()? Give example?

The strpos() function finds the position of the first occurrence of a string inside another string. The stripos() function finds the position of the first occurrence of a string inside another string. 2. It is case-sensitive function.

What does Strpos return if not found?

Return Value: Returns the position of the first occurrence of a string inside another string, or FALSE if the string is not found.


1 Answers

try preg match for multiple

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

strpos() with multiple needles?

like image 190
user3270784 Avatar answered Sep 17 '22 19:09

user3270784