函数来检查数组strpos并返回一个数组


function to check array strpos and return an array

你好,我想创建一个函数来检查etry是否包含一些单词。

对于我的登录脚本,我想创建一个函数来检查$_POST中是否有一些关键字。

因此,我想创建一个数组,其中包含我想要的单词:

function value_check($a, $b){
    $haystack = array ($a, $b)
    $words = array ("abc", "def");
    if(strpos($haystack, $words) === true) { 
        return ($a or $b, or both where strpos === true);
    }
    return false;
}

我想通过以下方式调用该函数:

$valid_value = value_check($a, $b);
if ($valid_value['a'] === true) {
 //do something
}
if ($valid_value['b'] === true) {
 //do something
}

非常感谢。

好的,为了澄清我的问题,我想缩短我的代码。而不是使用:

...else if ($a === "abc" || $a === "Abc"  ) {
        $errors['a'][] = "text";
}else if ($b === "def" || $a === "Def"  ) {
        $errors['b'][] = "text";
    }  

我想,在使用一个函数时,我可以更轻松地检查该数组中是否有特定的字符串。希望现在一切都会明朗起来。谢谢

读取此in_array以在数组中搜索。这就像Ascherer建议的那样,用于从字符串创建数组。

function value_check ($haystack) {
    foreach ($words as $element) {
        if (in_array($element,$haystack) {
            $result[] = $element;
        }
    }
    return $result;
}

呼叫

$somestuff = array($a,$b);
$valid_value = value_check ($somestuff);
foreach ($valid_value as $value) {
    // do something
}