Search code examples
phpregexfilter-var

php code with filter_var_array()/ filter_input_array not working properly


A test of the php code below with ideone.com gives 2 errors

"PHP Warning: filter_var_array(): 'regexp' option missing in /home/RhLGYU/prog.php on line 18"

The line is $myinputs = filter_var_array($data, $args).

It may be my beginners experience but I dont understand the error message, e.g., I dont know what goes wrong in the code below. I checked the 2 regex expressions definitions (REGEXP_MULTIPLE_NAMES, REGEXP_PHONE_NL) and they are okay.

I suspect there is an error in the definition of $args, but I cant find it (when I compare with examples like in http://www.w3schools.com/Php/filter_validate_regexp.asp).

PHP code:

define("REGEXP_MULTIPLE_NAMES", "/^[a-zA-Z\s-]+$/i");

//Expression to check Dutch phone numbers. Number must start with zero and number of digits should be 10. Different area and country codes are allowed.
define("REGEXP_PHONE_NL", "/(^\+[0-9]{2}|^\+[0-9]{2}\(0\)|^\(\+[0-9]{2}\)\(0\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\-\s]{10}$)/"); 

$data = array(
    'city' => 'The hague',
    'telephonenr' => '0701234567'
);
$args = array(
    'city' => array(
        'filter' => FILTER_VALIDATE_REGEXP,
        array(
            "options" => array(
                "regexp" => REGEXP_MULTIPLE_NAMES
            )
        )
    ),
    'telephonenr' => array(
        'filter' => FILTER_VALIDATE_REGEXP,
        array(
            'options' => array(
                "regexp" => REGEXP_PHONE_NL
            )
        )
    )
);

$myinputs = filter_var_array($data, $args);
print_r($myinputs);

Solution

  • Looking at the php manual i think its suppose to be structured like:

    $args = array(
    'city' => array(
        'filter' => FILTER_VALIDATE_REGEXP,
        'options' => array(
            "regexp" => REGEXP_MULTIPLE_NAMES
        )
    ),
    'telephonenr' => array(
        'filter' => FILTER_VALIDATE_REGEXP,
        'options' => array(
            "regexp" => REGEXP_PHONE_NL
        )
      )
    );