PHP: intval() equivalent for numbers >= 2147483647 PHP: intval() equivalent for numbers >= 2147483647 php php

PHP: intval() equivalent for numbers >= 2147483647


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

<?phpfunction 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 TESTINGecho '"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!


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?>


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