A.F.
A.F.

Reputation: 41

two possible filters for entry of filter_input_array (php)

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:

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?

Upvotes: 1

Views: 191

Answers (1)

Barmar
Barmar

Reputation: 781750

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.

Upvotes: 1

Related Questions