Alex
Alex

Reputation: 87

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

<?php
if(isset($_POST['QueryCode'])){ 

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

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

Upvotes: 0

Views: 92

Answers (1)

jakub wrona
jakub wrona

Reputation: 2254

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;

Upvotes: 1

Related Questions