我们在PHP中有一个函数in_array
,它检查给定的值是否存在于数组中。我想我想反过来。我有一组字符串:
$words = array("hello", "world");
并且,我想通过给出all
或any
的参数来检查给定字符串是否包含这些单词。
$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;
}
?>
/**
* 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()
部分和所有部分中获得更好的表现。有什么可以改进吗?
<?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;
}
}
?>
答案 0 :(得分:4)
<?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;
}
?>
与我下面的人一样的代码与休息;添加。我没有足够的代表发表评论
答案 1 :(得分: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;
}
?>
答案 2 :(得分:1)
使用strpos不起作用,因为对于部分字符串匹配,它将返回true。搜索&#34; in&#34; in&#34; insersection&#34;导致误报。 这是从目标字符串中提取单词并在数组之间进行交叉的替代方法。
/**
* 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;
}
答案 3 :(得分:0)
使用strpos()
代替substr()
或strstr()
,因为您只关心存在的字,您不需要解析任何内容。根据文档,它更快。
来自Manual:
如果您只想确定特定针是否出现在haystack中,请使用速度更快,内存更少的函数strpos()代替。
答案 4 :(得分:0)
您需要初始化$isFound
,使用stripos()
进行不区分大小写的搜索,并检查stripos()
是否返回false
,而不是0.(0表示{{1}以$string
)
$value