Using an array as needles in strpos

php strpos array
php strpos case-insensitive
php startswith
strpos sql
searching string in php
strpos in codeigniter
str_replace
strpos in cakephp

How do you use the strpos for an array of needles when searching a string? For example:

$find_letters = array('a', 'c', 'd');
$string = 'abcdefg';

if(strpos($string, $find_letters) !== false)
{
    echo 'All the letters are found in the string!';
}

Because when using this, it wouldn't work, it would be good if there was something like this

@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($haystack, $needle, $offset=0) {
    if(!is_array($needle)) $needle = array($needle);
    foreach($needle as $query) {
        if(strpos($haystack, $query, $offset) !== false) return true; // stop on first true result
    }
    return false;
}
$string = 'Whis string contains word "cheese" and "tea".';
$array  = array('burger', 'melon', 'cheese', 'milk');
var_dump(strposa($string, $array)); // will return true, since "cheese" has been found

strpos - Manual, PHP: strpos_array is strpos that can take an array as needle. Raw. strpos_array.​php. <?php. function strpos_array($haystack, $needles, $offset = 0) {. MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.

str_replace is considerably faster.

$find_letters = array('a', 'c', 'd');
$string = 'abcdefg';
$match = (str_replace($find_letters, '', $string) != $string);

PHP: strpos_array is strpos that can take an array as needle · GitHub, from the $compare_text and $array want to compare the array value in strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) is it possible to use array in  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

The below code not only shows how to do it, but also puts it in an easy to use function moving forward. It was written by "jesda". (I found it online)

PHP Code:

<?php
/* strpos that takes an array of values to match against a string
 * note the stupid argument order (to match strpos)
 */
function strpos_arr($haystack, $needle) {
    if(!is_array($needle)) $needle = array($needle);
    foreach($needle as $what) {
        if(($pos = strpos($haystack, $what))!==false) return $pos;
    }
    return false;
}
?>

Usage:

$needle = array('something','nothing');
$haystack = "This is something";
echo strpos_arr($haystack, $needle); // Will echo True

$haystack = "This isn't anything";
echo strpos_arr($haystack, $needle); // Will echo False 

How to use array in $needle for strpos() function using PHP , For performance reasons, it wraps around strpos and stripos when $needles is not an array. The second is that same fully-featured version with  PHP: strpos_array is strpos that can take an array as needle.

You can iterate through the array and set a "flag" value if strpos returns false.

$flag = false;
foreach ($find_letters as $letter)
{
    if (strpos($string, $letter) === false)
    {
        $flag = true;
    }
}

Then check the value of $flag.

A PHP strpos variant taking an array of needles: strposm, It takes string as haystack and array of needles and returns false if none of needles appeared in haystack or key of array of first needle that appeared in string: <? I'm writing a new answer which hopefully helps anyone looking for similar to what I am. This works in the case of "I have multiple needles and I'm trying to use them to find a singled-out string". and this is the question I came across to find that.

If you just want to check if certain characters are actually in the string or not, use strtok:

$string = 'abcdefg';
if (strtok($string, 'acd') === $string) {
    // not found
} else {
    // found
}

PHP (strpos function for arrays), How could i select from multiples needles in a haystack using the below code:? Your function should loop around each element of the array (lookup 'foreach' in the All I need to do is go 'php strstr' in the address bar and the 'strstr()' page in  * A strpos variant that accepts an array of $needles - or just a string, * so that it can be used as a drop-in replacement for the standard strpos, * and in which case it simply wraps around strpos and stripos so as not * to reduce performance. * * The "m" in "strposm" indicates that it accepts *m*ultiple needles. *

How to use multiple 'needles' in haystack? - PHP, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior #22 raknjak opened this issue Jan 9, 2019 · 2 comments

PHP strpos() Function, My version of strpos with needles as an array. Also allows for a string, or an array inside an array. <?php function strpos_array($haystack, $needles) { Clone via HTTPS Clone with Git or checkout with SVN using the repository’s web address.

strpos, int stripos ( string haystack, string needle [, int offset]). Strpos(), and its case-​insensitive sibling stripos(), returns the index of the first occurrence of a substring​  I'm trying to exclude several urls from appending a nofollow rule using strpos but it only works with 1 url, I can't get it to exclude all the urls I need to include. This is the code that I've tried:

Comments
  • @Arnaud, what is your suggested implement?
  • I'm not sure, but maybe we could advance in the string from $offset, and stop when the first of the needles is found. Think about a big text full of "a". Take $needles = [a, b]. Your function strposa will run through all of the text, but it is not necessary ! Am I understandable ?
  • Thanks @Arnaud for the feature suggestion! I totally agree with your suggestion importance and have updated my answer with improved code example.
  • This is not exactly what I wanted to say, because with your new function $needles = [b, a] has yet a problem, and in addition the function no longer returns the position of the first occurrence. Let's me explain a little more. Assume that the string is "ABCDEF", and the needles are C and F. What we could do is running through the string : A, B... C ! We detect C, so we stop here and we can return the position of the first occurrence of a needle, which is 2. It could work with more than single-character strings, but I've not thought about the exact implementation.
  • I improved the code by changing foreach($needle as $k => $query) { if(strpos($haystack, $query, $offset) !== false) return $k; }, so it returns the key of the matching item for further handling.
  • syntax error: 1 closing bracket too much at line #3 (I couldn't correct it directly in the code above due to the > 6 character edit requirement ;))
  • Oops, that was off the cuff. Fixed now. Cheers for the heads up!
  • one of the greatest solutions!
  • This worked perfectly for my purposes.
  • I believe this only returns the 1st position it finds. Any suggestions for how to tweak it to return the position of every needle in the haystack?
  • shouldn't it be !== flase ?
  • It should be !==false. Unless you break right after you set your flag to true. And then would have to interpret the flag as a Warning that a needle is not in the haystack. That means that what you are trying to achieve is to check that all your needles are in the haystack. Which is not the question. So.. yeah. !== false
  • Incredible answer - MUCH faster execution than multiple strpos(), e.g., if (strpos($string, "a") !== false && strpos($string, "c") !== false && strpos($string, "d") !== false)
  • This worked perfectly for me since i was looking my array contained sub-strings. I was saved from writing sql command like"%$string%"