Strpos() with Multiple Needles

Checking for multiple strpos values

I just used the OR statement (||)

<?php 
if ((strpos($color,'1') || strpos($color,'2') || strpos($color,'3')) === true)
{
//do nothing
} else {
echo "checked";
}
?>

Using 2 or more needles when using strpos

You can't use two needles in strpos. But what you can do, is use it twice, with an or:

function find_excluded_url ($url) {
return (strpos($url, "tumblr.com")!==false) || (strpos($url, "google.com")!==false);
}

Shorter way to write strpos with multiple needles in the haystack in PHP

Use a regular expression:

if (preg_match('#/(?:cgi-bin|css|images|includes|test)#', $dir)) {
continue;
}

PHP strpos match all needles in multiple haystacks

You could use an intersection of found sentences per word:

$found = array();

foreach ($words as $word) {
$found[$word] = array_filter($sentences, function($sentence) use ($word) {
return strpos($sentence, $word) !== false;
});
}

print_r(call_user_func_array('array_intersect', $found));

Or, approach from $sentences:

$found = array_filter($sentences, function($sentence) use ($words) {
foreach ($words as $word) {
if (strpos($sentence, $word) === false) {
return false;
}
}
// all words found in sentence
return true;
});

print_r($found);

One important thing to mention is that your search criteria was wrong; instead of strpos($sentence, $word) you should explicitly compare against false, otherwise you will miss a match at the start of a sentence.

Multiple strpos possible?

How about:

if (preg_match('/\{.*?:.*?\}/', $string)) {
echo 'string is good.';
}

Where:

/
\{ : openning curly brace (must be escaped, it' a special char in regex)
.*? : 0 or more any char (non greeddy)
: : semicolon
.*? : 0 or more any char (non greeddy)
\} : closing curly brace (must be escaped, it' a special char in regex)
/

Define multiple needles using stripos

Create a function that loops through an array?

function check_matches ($data, $array_of_needles)
{
foreach ($array_of_needles as $needle)
{
if (stripos($data, $needle)!==FALSE)
{
return true;
}
}

return false;
}

if (check_matches($data, $array_of_needles))
{
//do the rest of your stuff
}

--edit added semicolon

Using an array as needles in strpos

@Dave an updated snippet from http://www.php.net/manual/en/function.strpos.php#107351

function strposa($haystack, $needles=array(), $offset=0) {
$chr = array();
foreach($needles as $needle) {
$res = strpos($haystack, $needle, $offset);
if ($res !== false) $chr[$needle] = $res;
}
if(empty($chr)) return false;
return min($chr);
}

How to use:

$string = 'Whis string contains word "cheese" and "tea".';
$array = array('burger', 'melon', 'cheese', 'milk');

if (strposa($string, $array, 1)) {
echo 'true';
} else {
echo 'false';
}

will return true, because of array "cheese".

Update: Improved code with stop when the first of the needles is found:

function strposa(string $haystack, array $needles, int $offset = 0): bool 
{
foreach($needles as $needle) {
if(strpos($haystack, $needle, $offset) !== false) {
return true; // stop on first true result
}
}

return false;
}
$string = 'This string contains word "cheese" and "tea".';
$array = ['burger', 'melon', 'cheese', 'milk'];
var_dump(strposa($string, $array)); // will return true, since "cheese" has been found

Strpos 2 variables

Important to remember that strpos() returns the index of the string, which can be zero, which evaluates to false if you're not checking properly. Always compare strictly and dont use an or operator when you want to check if both conditions are true.

if (strpos($string,'Good') !== false && strpos($string,'Excellent') !== false) {
$pid= '1';
} else {
$pid= '0';
}

Or, more succinctly using a ternary:

$pid = (strpos($string,'Good') !== false && strpos($string,'Excellent') !== false) ? 1 : 0;

Just to expand on the use of strpos, consider this code, which returns "no," because "Good" is at the zeroth position.

$string = "Good morning";
if (strpos($string, "Good")) {
echo "yes";
} else {
echo "no";
}

From the manual:

Warning

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.



Related Topics



Leave a reply



Submit