0

I know you can use arrays with preg_replace but I need to match 2 arrays. 1 array is with forbidden chars and the other is with the variables I want it to find in. Basically:

$invalidChars = array("#/#", "#\\#", "#'#", "#\"#");
$matchIn = array("var1" => $var1 , "var2" => $var2);

If I do the following to match them:

if(preg_match($invalidChars, $matchIn)){
   echo "Invalid chars found";
   die();
}else{
   "No invalid chars";
   die();
}

then PHP throws me the following error

 Warning: preg_match() expects parameter 2 to be string, array given

If it's not possible to use preg_match with 2 arrays how would I check if the variables contain one or more of the invalid charachters?

SirMaxime
  • 154
  • 1
  • 4
  • 9
  • Please try to use this one, from stackoverflow: http://stackoverflow.com/questions/8627334/how-to-search-in-an-array-with-preg-match – Vyacheslav Jul 30 '16 at 22:01

3 Answers3

1

preg_match() can only accept strings as pattern. There is no reason for it to support arrays, since a single regular expression pattern can be made to match all those characters.

$invalidChars = "#[/\\\\'\"]#";

The second parameter can also only be a string. In this specific case you can just concatenate the strings to test them all, since you are looking for individual characters:

if (preg_match("#[/\\\\'\"]#", implode('', $matchIn))
{
    ...

But normally you would have to iterate through the subjects and test them individually:

foreach ($matchIn as $subject)
    if (preg_match("#[/\\\\'\"]#", $subject))
    {
        ...
Havenard
  • 27,022
  • 5
  • 36
  • 62
  • Oh, yes, obviously. Thanks, but what about the second array with all my variables? Would I have to preg_match each variable seperatly – SirMaxime Jul 30 '16 at 22:02
  • 2
    *"But normally you would have to iterate through the subjects..."*: you can use `preg_grep` http://php.net/manual/en/function.preg-grep.php – Casimir et Hippolyte Jul 30 '16 at 23:53
  • To figure a literal backslash in a pattern string you need 4 backslashes. You can only use 3 backslashes if the next character isn't the second part of an escape sequence *(you can write `\\\'` but not `\\\n`)*. – Casimir et Hippolyte Jul 31 '16 at 00:06
  • @CasimiretHippolyte It was actually two backlashes to represent `\ `, but in any case I think you are right. In order to match `\ ` have to deliver `\\ ` in the pattern, and therefore `\\\\ ` because I have to escape it for the PHP parser aswell. – Havenard Jul 31 '16 at 02:05
0

try this funciton,

function validate($invalidChars,$matchIn){
    foreach($invalidChars as $invalidChar){
        if(preg_match($invalidChar, $matchIn)){
            echo "Invalid chars found";
            return;
        }
    }
        echo "No invalid chars";
        return;
}
LF00
  • 27,015
  • 29
  • 156
  • 295
0

I did something similar. Here's a slight variation of the code I used to solve the problem :

public function remove_value_from_array($val, &$arr) {
   array_splice($arr, array_search($val, $arr), 1);
}

public function filter($invalidChars, $matchIn){
   $values = array_values($matchIn);
   /*Then you build the regular expression you will need for filtering.*/
   /*With $invalidChars = array("#/#", "#\\#", "#'#", "#\"#"), you will get "#/#|#\\#|#'#|#\#"*/
   $regex = implode("|", $invalidChars);
   $filtered_values = array_filter($values, function($value) use ($regex){
      preg_match ('/'.$regex.'/i', $value, $matches); return count($matches) > 0;
   });

   foreach($filtered_values as $value){
      remove_value_from_array($value, $matchIn);
   }

   return $matchIn;


}

But you would need to escape the $invalidChars array values as they contain special characters (#, ...).

iceberg53
  • 330
  • 1
  • 7