Alex Alex - 1 year ago 54
MySQL Question

Php code to match string if any word is in array

I have been trying to match post query sting if it contain any word in array.
I try to execute sql query using the command to update or select but i don't want to drop or delete row and table.

Please bellow code is not matching the sting in array well


$RequestQuey = htmlspecialchars($_POST['QueryCode']);
$sqlchecker = strtolower($RequestQuey);
$bads_arrays = array(

foreach ($bads_arrays as $strings){
if (strpos($sqlchecker , $strings) !== false) {
echo "Match found";
return false;
echo "Not found!";
return true;

Answer Source

You are doing a strtolower() on the examined string but you have uppercase words in the array. So either replace strpos with stripos or change the words in the array to be lowercase or convert the examined string ($sqlchecker) to uppercase.

$anyMatched = false;
foreach ($bads_arrays as $strings){
    if (stripos($sqlchecker , $strings) !== false) {
        $anyMatched = true;

echo ($anyMatched ? 'Match found' : 'Not Found');
return $anyMatched;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download