Maybe you can do 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 check which words of the second array
are found in the first. Thus, it returns which are values found in the first array, which are in the second. I apply a sort
in both, for the ordering to be similar. Then I compare the two with ===
. If true, it's because all the words you're looking for are in the list you've separated.
So you can understand a little about array_intersect
, I'll give you some examples:
array_intersect(['a', 'b'], ['a']); // ['a']
array_intersect(['a'], ['a', 'b']); // ['a'] ('a' presente no segundo está no primeiro