"1 ----- 1 - + - 1" is a valid integer value in PHP?

9

I am testing a function for PHP value filter called filter_var() . And a of your filters , focused on int values seems to accept any combination of numbers, + and - . Example:

filter_var("1teste2", FILTER_SANITIZE_NUMBER_INT) // 12
filter_var("1-teste-2", FILTER_SANITIZE_NUMBER_INT) // 1--2
filter_var("1--1--+--1", FILTER_SANITIZE_NUMBER_INT) // 1--1--+--1

Is this normal behavior? Does the language accept this type of value as int with no problems, or is it a bug of the filter function?

    
asked by anonymous 15.01.2015 / 23:20

2 answers

10

According to documentation , yes. You know how it is, it's PHP, things are made to work more or less. And the function does what it promises even though the programmer's expectation is another:

  

Remove all characters except digits, plus and minus sign.

The flag function does not promise to transform the string into a valid integer, it promises only to remove all characters other than digits and minus signs. more.

Then the title question should be answered with a no. But the function used does not have the task of solving this. The flag name suggests something it does not do.

See no examples of the flag that probably caters for you.

Documentation .

    
15.01.2015 / 23:26
10

According to the documentation, this behavior is expected. In case FILTER_SANITIZE_NUMBER_INT :

  

Remove all characters except digits, plus and minus sign.

     

Removes all characters except digits ([0-9]), plus (+) and minus (-) signs.

    
15.01.2015 / 23:26