Home > Net >  two possible filters for entry of filter_input_array (php)
two possible filters for entry of filter_input_array (php)

Time:01-13

I would like to check if a value of an array for example is either an email adress or fits in a Regex.

If there are two filters that would together do exactly what I want:

  • is it possible to use these two existing filters and if the first one doesn't fit, check if the second one does?
  • or do I have to create a filter callbac anyways?

For creating a usecase: A Login situation, where you either can use an emailadress or a username, but the input field for both is the same.

Pseudocode:

    $options = [
        'username' =>
        [
            'options' => [
                
                'filter' => FILTER_VALIDATE_EMAIL | FILTER_VALIDATE_REGEXP,
                'regexp' => '/([A-Za-z0-9_-]{1,25})/'],
        ],
    ];


    $filteredPost = filter_input_array(INPUT_POST, $options);

If something like this is possible, how?

CodePudding user response:

I don't think you can combine multiple filters like this -- you can only use | to combine a filter with flags.

In addition, you haven't formed the options array correctly. The regexp needs to be nested further.

$options = [
    'username' => [
        'filter' => FILTER_VALIDATE_EMAIL | FILTER_VALIDATE_REGEXP,
        'options' => [
            'regexp' => '/([A-Za-z0-9_-]{1,25})/'],
    ],
];

Each element of $options is an array containing filter and options keys.

  •  Tags:  
  • Related