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 8904607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:10:34+00:00 2026-06-15T02:10:34+00:00

We have a function in PHP, in_array , which checks if the given value

  • 0

We have a function in PHP, in_array, which checks if the given value is present in an array. I guess I wanna do a reverse of it. I have a set of strings:

$words = array("hello", "world");

And, I wanna check, if a given string has these words, by giving parameters whether all or any.

$string = "Hello, World!";
$second = "Hello and Welcome!";
in_string($words, $string, "all");
in_string($words, $string, "any");

What I have right now is, using stripos(). I do not wanna use regex.

Current Code:

<?php
    /**
    * Checks if the given words is found in a string or not.
    * 
    * @param Array $words The array of words to be given.
    * @param String $string The string to be checked on.
    * @param String $option all - should have all the words in the array. any - should have any of the words in the array
    * @return boolean True, if found, False if not found, depending on the $option
    */
    function in_string ($words, $string, $option)
    {
        if ($option == "all")
            foreach ($words as $value)
                $isFound = $isFound && (stripos($string, $value) || false);
        else
            foreach ($words as $value)
                $isFound = $isFound || (stripos($string, $value) || false);
        return $isFound;
    }
?>

My question is, can this be improved? Any thoughts?


Update #1: Current Code Updated:

/**
* Checks if the given words is found in a string or not.
* 
* @param Array $words The array of words to be given.
* @param String $string The string to be checked on.
* @param String $option all - should have all the words in the array. any - should have any of the words in the array
* @return boolean True, if found, False if not found, depending on the $option
*/
function in_string ($words, $string, $option)
{
    if ($option == "all")
    {
        $isFound = true;
        foreach ($words as $value)
            $isFound = $isFound && (stripos($string, $value) !== false);
        return $isFound;
    }
    else
    {
        $isFound = true;
        foreach ($words as $value)
            if (stripos($string, $value) !== false) return true;
        return $isFound;
    }
}

Now the function is working as expected. But I need a better performance in the foreach() part and all. Anything possible for improvements?


Update #2: Improvements Added

<?php
    /**
    * Checks if the given words is found in a string or not.
    * 
    * @param Array $words The array of words to be given.
    * @param String $string The string to be checked on.
    * @param String $option all - should have all the words in the array. any - should have any of the words in the array
    * @return boolean True, if found, False if not found, depending on the $option
    */
    function in_string ($words, $string, $option)
    {
        if ($option == "all")
        {
            foreach ($words as $value)
                if (stripos($string, $value) === false)
                    return false;
            return true;
        }
        else
        {
            foreach ($words as $value)
                if (stripos($string, $value) !== false)
                    return true;
            return false;
        }
    }
?>
  • 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-06-15T02:10:35+00:00Added an answer on June 15, 2026 at 2:10 am
    <?php
    /**
    * Checks if the given words is found in a string or not.
    * 
    * @param Array $words The array of words to be given.
    * @param String $string The string to be checked on.
    * @param String $option all - should have all the words in the array. any - should have any of the words in the array
    * @return boolean True, if found, False if not found, depending on the $option
    */
    function in_string ($words, $string, $option)
    {
        if ($option == "all") {
            $isFound = true;
            foreach ($words as $value) {
                $isFound = $isFound && (stripos($string, $value) !== false); // returns boolean false if nothing is found, not 0
                if (!$isFound) break; // if a word wasn't found, there is no need to continue
            }
        } else {
            $isFound = false;
            foreach ($words as $value) {
                $isFound = $isFound || (stripos($string, $value) !== false);
                if ($isFound) break; // if a word was found, there is no need to continue
            }
        }
        return $isFound;
    }
    ?>
    

    Just the same code as the person below me, with the break; added. I don’t have enough rep to comment apparently

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have PHP function which checks to see if variables are set and then
I have a function in PHP which calculates the distance between two places. Here
I have a php function which returns JSON in response to a ajax request
I have the following function in a PHP script, which returns and API call:
Right now I have a function which takes my uploaded file, checks the extension,
I have a function which returns only one row as array. I give a
I have a function in PHP language to create an xml file when requested.
I have created a function in PHP that calls a webservice and parses through
In PHP I have a function, the problem is it will output an extra
I have a question regarding static function in php. let's assume that I have

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.