if (isset($_POST['txt'])) {
if (!empty($_POST['txt'])) {
$input = $_POST['txt'];
if (preg_match('/^[a-zA-Z\s]+$/', $input)) {
$veggies = array("Potato", "Cucumber", "Carrot", "Orange", "Green Beans", "onion");
$fruits = array("Apple", "Banana", "orange", "Pineapple", "Grapes", "Watermelon");
$salad = array_merge($veggies, $fruits);
$search = array_filter($salad, function($list) use ($input) {
return (stripos($list, $input) !== FALSE);
});
print_r($search);
} else {
echo 'Only letters and spaces are allowed.';
}
} else {
echo 'Enter an item.';
}
}
If you also allow numbers, you can use the regular expression '
/^[a-zA-Z0-9\s]+$/'!