Using an Array as Needles in Strpos

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 function in php and an array as needle

foreach( $allowedMimes as $mime ) {
if( strpos( 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' , $mime ) ) {
return true;
}
}

PHP strpos array

strpos() does not allow more than one needle, you can do this:

$key = "results";
$reportKey = array("base", "second", "third","latest");
$keyArray = array();

foreach($html->find('a') as $element)
{
if (strpos($element->href, $key) !== false){
if (
strpos($element->href, $reportKey[0]) !== false
|| strpos($element->href, $reportKey[1]) !== false
|| strpos($element->href, $reportKey[2]) !== false
|| strpos($element->href, $reportKey[3]) !== false
){
$keyArray[] = $element->href;
}
}
}

echo "<pre>" . print_r($keyArray) . "</pre> ";

You could also do your own function, this is only an example:

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

Searching for array of needle values with strpos()

If i understand your question correctly, an array is not needed. You can simply use strpos twice with an or (in php, ||) operator

if (strpos($string,'keyword1') || strpos($string,'keyword2')) {
$hasstring = "yes";
}

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.

how to strpos with array or in_array in multiple text string

Please try this one will give expected output.

$message = 'This domain has strpos';
$links = array('domain.com', 'do.main');
$safe = array();
foreach($links as $key=>$result){
if(strpos($message, $result) != false){
$safe[$result] = "String is available";
} else{
$safe[$result] = "String is not available";
}

}
print_r($safe);

Match a string in an array strpos(): Empty needle

You are doing the opposite way , searching string in 42 , instead go for

strpos($string, $imp)

TRY THIS : (updated)

<?php

$_SESSION['arry'] = array(236,235,239,243,246);

$imp = 239;
$arrys = $_SESSION['arry'];
$arrys = array_unique($arrys);
$pow = "";
print "<pre>";
print_r($arrys);
print "</pre>";
foreach($arrys as $string)
{
if($string == $imp) {
$pow = 1; echo "we have".$imp."in array<br/>";
}
else{
$pow = 0; echo "sorry".$imp."wasn't found in array<br/>";
}
}
?>

How to use an array as $haystack and $array[$i] (string) as needle in php?

in_array() uses a loose comparison to search for elements and will not yield the results you're looking for.

Instead, you need to use your own logic (using strpos() as you guessed) to implement the functionality you desire, like so:

foreach( $imglist as $image_key => $image)
foreach( $notes as $notes_key => $note)
if( !(strpos( $note, $image) === false))
echo "$image $note\n";

This will output:

12.jpg Main/folder/02/12.jpg = twelve
16.jpg Main/folder/02/16.jpg = sixteen

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



Related Topics



Leave a reply



Submit