3

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

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

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

and i would like to call that function by:

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

thanks alot.

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.

bonny
  • 3,147
  • 11
  • 41
  • 61
  • Was thinking of the same thing... – Manatax Feb 22 '13 at 18:31
  • Check the manual on `strpos`, it does not take arrays as arguments. – jeroen Feb 22 '13 at 18:32
  • @MarkoD obviously he don't know how to do it, and just need some help – Peter Feb 22 '13 at 18:32
  • @jeroen, I think the OP has offered it as pseudo code. Obviously there's some invalid PHP syntax there. – Jason McCreary Feb 22 '13 at 18:33
  • 1
    I would not suggest a function like the one you are proposing; it will require additional checks on the results to see if values are set to avoid php warnings; it would make your code unnecessarily complicated. If you are going to check both results in separate `if` statements anyway, I would just call the function twice with different input and have it return `true` or `false`. – jeroen Feb 22 '13 at 18:39
  • i updated my question to clarify. thanks. – bonny Feb 22 '13 at 18:57

1 Answers1

2

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

a call

$somestuff = array($a,$b);
$valid_value = value_check ($somestuff);
foreach ($valid_value as $value) {
    // do something
}
Manatax
  • 4,083
  • 5
  • 30
  • 40
  • 1
    that and `explode( ' ', $string )` could be helpful – Ascherer Feb 22 '13 at 18:34
  • While a good general recommendation, this will not solve the OP's *exact* needs. – Jason McCreary Feb 22 '13 at 18:34
  • @JasonMcCreary Well... there is no specific question... more like, "I don't know how to do it"... therefore a link to the manual that will teach him/her how to do it is the most accurate answer – Manatax Feb 22 '13 at 18:37
  • i tried using: if(in_array($words, $haystack) === true) { return (true); } but still does not work? thanks. – bonny Feb 22 '13 at 19:33
  • Agreed that the question is unclear. However, simply linking to the documentation is not a good answer. – Jason McCreary Feb 22 '13 at 20:21
  • @JasonMcCreary Let's agree to disagree. I would personally prefer to be pointed out in the right direction rather than have the code done for me. – Manatax Feb 22 '13 at 23:34
  • @bonny in_array() is case-sensitive. – Manatax Feb 22 '13 at 23:39
  • @bonny and try return in_array($words, $haystack); – Manatax Feb 22 '13 at 23:43
  • Clearly your answer is not helpful as proven by this comment thread. – Jason McCreary Feb 23 '13 at 14:06
  • @JasonMcCreary He can ask further questions in the comments if he did not find the answer sufficiently clear... just like he did. Besides, how is my answer less constructive than yours? If you believe that a better answer can be provided, then feel free to do so, instead of complaining about mine. – Manatax Feb 23 '13 at 23:12