25

Possible Duplicate:
Check if a variable is a natural number

Just came across where I need to sanitize an input and remove all non-digit characters with PHP. Thought of making a function myself, but just first wanted to check up on the possible built-in functions, so I won't have to spend extra time reinventing the wheel.

Community
  • 1
  • 1
Lucas
  • 16,930
  • 31
  • 110
  • 182

5 Answers5

35

There isn't a direct equivalent to pareseInt() in PHP because PHP doesn't have a NaN data type. parseInt() will produce NaN if the value can't be parsed.

In addition, parseInt() has the radix parameter, which none of the PHP options give you.

But that said, if all you want to do is turn a string into an integer, then there are several options:

$int = (int)$string;

$int = intval($string);

$int = settype($string,'integer');

Those three will all work in much the same way. There may be some edge cases where intval() differs from the other two, but in general, they will turn a string into an int. In the absence of NaN, they all output 0 (zero) if the string is non numeric and can't be parsed. But this should be sufficient for most DB sanitation purposes.

If you really want a the NaN on error, the closest you'll get is null. You can get this using filter_var():

$value = filter_var(FALSE, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);

If you just want to check that the string is numeric, the is_numeric() function can do that:

$bool = is_numeric($string);

However, it returns true for decimals and also for scientific notation, so may not be perfect for most situations. A better option is ctype_digit() which returns true if the string is made up only of digits:

$bool = ctype_digit($string);

If non e of this suits, there is always the regex option, using preg_match() or preg_replace(), but these are definitely overkill for this kind of scenario.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • Using (int) and intval() are very error-proof, but be aware that if the string represents a floating point number (i.e. has decimals), then these functions simply remove whatever comes after the point. So "3.999" becomes 3. – Wytze Sep 26 '13 at 13:05
  • @Wytze: true, but the question was asking specifically to remove non-digit characters, so the assumption would be that it isn't a decimal. There are other options if you need to handle decimals. – Spudley Sep 26 '13 at 13:09
32

You can use regular expression as below

$int = (int) preg_replace('/\D/', '', $strNonNumeric);

OR

$int = (int) preg_replace('/[^0-9]/', '', $strNonNumeric);
Lucas
  • 16,930
  • 31
  • 110
  • 182
GBD
  • 15,847
  • 2
  • 46
  • 50
  • ah, exactly what I wanted. Thanks a lot. – Lucas Sep 29 '12 at 07:32
  • 2
    @think123 - it's worth pointing out that (a) this does not produce an int; it's still a string, albeit only containing digits, and (b) regex is overkill for this scenario. – Spudley Sep 29 '12 at 08:06
  • 1
    True, but this is one of the ones that actually works. I'll edit it all the same. – Lucas Sep 29 '12 at 08:09
  • WARNING! This is very dangerous beacuse numbers in the same string will be read as one. (es. asd25asd56aa -> 2556) – LoxLox Sep 08 '14 at 15:44
5

You can use:

$string = "90"; //string
$myInt = (int) $string; //parse to int
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
  • Sorry, but that's not exactly what I want. `(int)` won't remove all non-digit characters, it will just return `0` if there are any non-digit characters inside the string. – Lucas Sep 29 '12 at 07:28
  • 4
    @think123: Type casting `(int)` will do the same as `parseInt()` in javascript. This is what you asked, isn't it? – Glavić Sep 29 '12 at 07:45
  • Straight to the point. – Sergio A. Feb 07 '20 at 09:22
3

I would recommend using FILTER_VALIDATE_INT Filter.

about Filters on php.net

0

You can also use sscanf (just like you do in C)

<?php

    $str = "10";

    sscanf($str, "%d", $intVal);
    $intVal *= 10;

    echo $intVal;
?>

Output:

100

enhzflep
  • 12,927
  • 2
  • 32
  • 51