功能来检查数组strpos和返回数组 [英] function to check array strpos and return an array

查看:504
本文介绍了功能来检查数组strpos和返回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想创建检查如果etry包含一些词的功能。

hello i would like to create a function that checks if an etry contains some words.

我的登录脚本我想创建一个检查,如果$ _ POST有一些关键词在它的功能。

for my login-script i would like to create a function that checks if the $_POST has some keywords in it.

therfor我曾经想过创建一个包含我正在寻找这样的话数组:

therfor i have thought to create an array that contains the words i'm looking for like that:

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
}

非常感谢。

好吧,澄清我的问题我想缩短我的code。而不是使用:

Okay, to clarify my question i would like to shorten my code. instead of using:

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

我想我可以同时使用,检查容易,如果有数组中一个特定字符串的函数做更舒服一点。希望这将是清楚了。谢谢。

i thought i can do it a little bit more comfortable while using a function that checks easily if there is a that specific string in that array. hope it will be clear now. thanks.

推荐答案

阅读 in_array 在阵列搜索。这爆炸从一个字符串创建一个数组一样Ascherer建议。

Read this in_array for search in array. And this explode for creating an array from a string like Ascherer suggested.

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
}

这篇关于功能来检查数组strpos和返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆