Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7430217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:06:11+00:00 2026-05-29T09:06:11+00:00

How do you use the strpos for an array of needles when searching a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:06:12+00:00Added an answer on May 29, 2026 at 9:06 am

    @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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use strpos to find out if a string exists and
When I use strpos it returns false if the both strings are equal. Is
How can you search a partial string when typing (not to use MySQL) like
When I use PHP header redirection, all session variables are lost... Some people say
I use the below function found in Highlight keywords in a paragraph for highlighting
When I use strpos() it is case sensitive. Why? Isn't it not supposed to
i am trying to use this plugin ( the multiple birds (remote) example), but
Hello people :) I'm trying to use strpos() to find whole words in a
Huh, looking at all those string functions, sometimes I get confused. One is using
I am really new to C. I want to use the strpos function but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.