检查字符串中是否包含单词数组

Pra*_*man 2 php performance substring find

我们在PHP中有一个函数in_array,该函数检查给定值是否存在于数组中。我想我想做相反的事情。我有一组字符串:

$words = array("hello", "world");
Run Code Online (Sandbox Code Playgroud)

而且,我想通过给定参数all或来检查给定的字符串是否包含这些单词any

$string = "Hello, World!";
$second = "Hello and Welcome!";
in_string($words, $string, "all");
in_string($words, $string, "any");
Run Code Online (Sandbox Code Playgroud)

我现在所拥有的是使用stripos()。我不想使用regex

当前代码:

<?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;
    }
?>
Run Code Online (Sandbox Code Playgroud)

我的问题是,这可以改善吗?有什么想法吗?


更新#1:当前代码已更新:

/**
* 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,该功能可以按预期运行。但是我需要一个整体上更好的表现foreach()。有什么改进的可能吗?


更新#2:添加了改进

<?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;
        }
    }
?>
Run Code Online (Sandbox Code Playgroud)

Mic*_*ael 5

<?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;
}
?>
Run Code Online (Sandbox Code Playgroud)

和我下面的人一样的代码,只是休息了;添加。我没有足够的代表来评论