PHP: How to sanitize a natural number (positive INT)?

From PHP sanitize filters, there is one option for sanitizing integers:

FILTER_SANITIZE_NUMBER_INT - delete all characters except digits, plus and minus.

If we use:

filter_var($var2San, FILTER_SANITIZE_NUMBER_INT);

This will clear the points .and commas ,, but the signs are +and -will remain. For example: ++++ --- 1.110,4 <b>m<sup>2</sup></b>disinfected before ++++---111042. Ideally, it filter_varwill return falsewhen the number is 0, that is, the number should be a natural number , or rather a positive integer.

So a FILTER_SANITIZE_NUMBER_NATURALwould be convenient ... Is there a workaround for this, or do I need RegExp?

+4
source share
1 answer

, filter_var:

filter_var($var2San, FILTER_VALIDATE_INT,
           array('options' => array('min_range' => 1)));

FILTER_ VALIDATE _INT PHP ( $options), filter_var , , FALSE . :

  • -1FALSE
  • 0FALSE
  • 11
  • + 1FALSE
  • +22
  • ++3FALSE
  • 4+FALSE
  • 5.6FALSE
  • 7,8FALSE

, , , ( - , , + ). +, .

, 2147483647, FALSE ( 32- ).

+7

Source: https://habr.com/ru/post/1598727/


All Articles