Maybe you can make it with something like this:
if(isset($_POST['palavra'])):
$palavra = $_POST['palavra'];
$separa = explode(' ', $palavra);
$palavrasChaves = array("você", "bem", "?");
$quaisContem = array_intersect($separa, $palavrasChaves);
sort($palavrasChaves);
sort($quaisContem);
if($quaisContem === $palavrasChaves):
echo "Bem e você ?";
endif;
endif;
I used array_intersect
to verify which words of the second array
are found in the first. Thus, it returns which values are found in the first array, which are in the second. I apply a sort
in both, for the ordering to be similar. I then compare the two with ===
. If true, it’s because all the words you searched for are on the list that you’ve broken up.
So you can understand a little bit about array_intersect
, I’ll make some examples:
array_intersect(['a', 'b'], ['a']); // ['a']
array_intersect(['a'], ['a', 'b']); // ['a'] ('a' presente no segundo está no primeiro
gave certinho, so the difference between the array_intersect and the in_array is that one searches only one string in an array, and the other searches an array within another ne ?
– Otavio Fagundes
Good use of
array_intersect
. Why is no comparison made using==
? thus thesort
maybe it’s not necessary.– stderr
@stderr would be needed yet...
– Wallace Maxters