views:

996

answers:

5

Hello!

In PHP 5, I use intval() whenever I get numbers as an input. This way, I want to ensure that I get no strings or floating numbers. My input numbers should all be in whole numbers. But when I get numbers >= 2147483647, the signed integer limit is crossed.

What can I do to have an intval() equivalent for numbers in all sizes?

Here's what I want to have:

<?php
$inputNumber = 3147483647.37;
$intNumber = intvalEquivalent($inputNumber);
echo $intNumber; // output: 3147483647
?>

Thank you very much in advance!

Edit: Based on some answers, I've tried to code an equivalent function. But it doesn't work exactly as intval() does yet. How can I improve it? What is wrong with it?

function intval2($text) {
    $text = trim($text);
    $result = ctype_digit($text);
    if ($result == TRUE) {
        return $text;
    }
    else {
        $newText = sprintf('%.0f', $text);
        $result = ctype_digit($newText);
        if ($result == TRUE) {
            return $newText;
        }
        else {
             return 0;
        }
    }
}
+2  A: 

Either use number_format($inputNumber, 0, '', '')

Or if you only want to check if its a whole number then use ctype_digit($inputNumber)

Don't use the proposed is_numeric, as also floats are numeric. e.g. "+0123.45e6" gets accepted by is_numeric

jitter
float will still overflow this.
Artem Russakovskii
Float overflow doesn't matter, so it seems to be useful. Please see my edit in the question.
+2  A: 

In addition to number_format, you can use sprintf:

sprintf("%.0f", 3147483647.37) // 3147483647

However, both solutions suffer from float overflow, for example:

sprintf("%.0f", 314734534534533454346483647.37) // 314734534534533440685998080
Artem Russakovskii
Thank you! Please have a look at my edit in the question. Is this a good equivalent function?
@Artem: Not only does it suffer from float overflow, it also does rounding. sprintf("%.0f", 1.5) returns 2, while intval(1.5) returns 1.
mercator
+4  A: 

you can also use regular expressions to remove everything after the intial numeric parts:

<?php
$inputNumber = "3147483647.37";
$intNumber = preg_replace('/^([0-9]*).*$/', "\\1", $inputNumber);
echo $intNumber; // output: 3147483647
?>
SztupY
I think there's an error with your regex, your code does not output anything at all.
Dustin Fineout
sorry. it has been fixed.
SztupY
Thank you! :) Your answer was important since Dustin Fineout used your regexp ...
A: 

Try this function, it will properly remove any decimal as intval does and remove any non-numeric characters.

<?php
function bigintval($value) {
  $value = trim($value);
  if (ctype_digit($value)) {
    return $value;
  }
  $value = preg_replace("/[^0-9](.*)$/", '', $value);
  if (ctype_digit($value)) {
    return $value;
  }
  return 0;
}

// SOME TESTING
echo '"3147483647.37" : '.bigintval("3147483647.37")."<br />";
echo '"3498773982793749879873429874.30872974" : '.bigintval("3498773982793749879873429874.30872974")."<br />";
echo '"hi mom!" : '.bigintval("hi mom!")."<br />";
echo '"+0123.45e6" : '.bigintval("+0123.45e6")."<br />";
?>

Here is the produced output:

"3147483647.37" : 3147483647
"3498773982793749879873429874.30872974" : 3498773982793749879873429874
"hi mom!" : 0
"+0123.45e6" : 0

Hope that helps!

Dustin Fineout
Thank you very much, that's exactly what I was looking for.
A: 

If you want to handle arbitrary-precision numbers, you will need to use the gmp extension. The function gmp_init(), for example, converts a string to a gmp resource object. the drawback is that you must use other functions of that extension to further process that object. Converting it back to a string is done using gmp_strval(), for example.

$gmpObject = gmp_init($string, 10);
if ($gmpObject === FALSE) {
    # The string was not a valid number,
    # handle this case here
}
echo gmp_strval($gmpObject);

You might want to only verify that the string is representing a valid number and use the string itself, if you do not intend to do any operations on the value. That can be done using a regular expression:

$containsInt = preg_match('/^\d+$/', $string);
# Or for floating point numbers:
$containsFloat = preg_match('/^\d+(.\d+)?$/', $string);
echo $string;

Another option is to use is_numeric(). But that function does more conversion than you might like. Quoting from docs of that function:

... +0123.45e6 is a valid numeric value ...

soulmerge