2

我们在 PHP 中有一个函数in_array,它检查给定值是否存在于数组中。我想我想反其道而行之。我有一组字符串:

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

而且,我想检查给定字符串是否包含这些单词,方法是提供参数是否allany.

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

我现在拥有的是,使用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;
    }
?>

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


更新 #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;
    }
}

现在该功能按预期工作。但我需要在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;
        }
    }
?>
4

5 回答 5

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

和我下面的人一样的代码,带中断;添加。我显然没有足够的代表发表评论

于 2012-11-27T07:49:35.693 回答
1

划分逻辑,您将在每个函数中获得更简单的代码,更好的性能,每个函数将解决具体任务。

<?php
function in_string_all ($string, $words) {
    $isFound = true;
    foreach ($words as $value) {
        $isFound = $isFound && stripos($string, $value) !== false;
        if (!$isFound) 
            break;
    }
    return $isFound;
}

function in_string_any ($string, $words) {
    foreach ($words as $value) 
        if (stripos($string, $value) !== false)
            return true;
    return false;
}
?>
于 2012-11-27T08:39:56.237 回答
1

使用 strpos 不起作用,因为它会为部分字符串匹配返回 true。在“insersection”中搜索“in”的示例会导致误报。这是从目标字符串中提取单词并在数组之间进行交集的替代方法。

/**
 * Checks if a array of given words is found in a string.
 * 
 * @param array $words The array of words to be given.
 * @param string $string The string to be checked on.
 * @param boolean $exactMatch true - should have all the words in the array. false - should have any of the words in the array
 * @param boolean $insensitive true to compare insensitive. false to compare sensitive
 * @return boolean True, if found, False if not found, depending on the $option
 */
private function arrayInString (array $words, $string, $exactMatch = true, $insensitive = true)
{
    $stringArr = explode(" ", preg_replace('/[^a-z0-9]+/i', ' ', $string));
    if ($insensitive) {
        $words = array_map('strtolower', $words);
        $stringArr = array_map('strtolower', $stringArr);
    }
    $intersectionCount = count(array_intersect($words, $stringArr));
    if ($exactMatch && $intersectionCount == count($words)) {
        return true;
    }
    if (!$exactMatch && $intersectionCount > 0) {
        return true;
    }

    return false;
}
于 2018-03-08T15:17:52.250 回答
0

您需要初始化$isFoundstripos()用于不区分大小写的搜索,并检查stripos()返回false,而不是 0。(0 表示以$string开头$value

function in_string ($words, $string, $option)
{
if ($option == "all") {
    $isFound = true;
    foreach ($words as $value) {
        $isFound = $isFound && (stripos($string, $value) !== false);
    }
} else {
    $isFound = false;
    foreach ($words as $value) {
        $isFound = $isFound || (stripos($string, $value) !== false);
    }
}
return $isFound;
}
于 2012-11-27T07:43:23.917 回答
0

使用strpos()代替substr()strstr()因为您只关心存在的单词,您不需要解析任何内容。根据文档,它更快。

手册

如果您只想确定某个特定的针是否出现在 haystack 中,请改用更快且内存占用更少的函数 strpos()。

于 2012-11-27T07:24:07.687 回答