PHP remove commas from numeric strings

146,624

Solution 1

Not tested, but probably something like if(preg_match("/^[0-9,]+$/", $a)) $a = str_replace(...)

Solution 2

Do it the other way around:

$a = "1,435";
$b = str_replace( ',', '', $a );

if( is_numeric( $b ) ) {
    $a = $b;
}

Solution 3

The easiest would be:

$var = intval(preg_replace('/[^\d.]/', '', $var));

or if you need float:

$var = floatval(preg_replace('/[^\d.]/', '', $var));

Solution 4

Try this .this worked for me

number_format(1235.369,2,'.','')

if you use number_format like this number_format(1235.369,2) answer will be 1,235.37

but if you use like below

number_format(1235.369,2,'.','') answer will be 1235.37

it's removing the "," of "1,235.37"

Solution 5

It sounds like the ideal solution for what you're looking for is filter_var():

$a = filter_var($a, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);

(Note that it's using FILTER_VALIDATE_FLOAT instead of FILTER_VALIDATE_INT because that one doesn't currently have a FILTER_FLAG_ALLOW_THOUSAND option).

Share:
146,624
user1082428
Author by

user1082428

Updated on July 05, 2022

Comments

  • user1082428
    user1082428 almost 2 years

    In PHP, I have an array of variables that are ALL strings. Some of the values stored are numeric strings with commas.

    What I need:

    A way to trim the commas from strings, and ONLY do this for numeric strings. This isn't as straightforward as it looks. The main reason is that the following fails:

    $a = "1,435";
    
    if(is_numeric($a))
        $a = str_replace(',', '', $a);
    

    This fails because $a = "1435" is numeric. But $a = "1,435" is not numeric. Because some of the strings I get will be regular sentences with commas, I can't run a string replace on every string.

  • user1082428
    user1082428 over 12 years
    Yes, this works. I tried all the string formats I would expect, and this works. Will mark as accepted answer once the site lets me.
  • KnightHawk
    KnightHawk over 8 years
    potential overhead of calling str_replace every time, but the absence of any regex is a big plus.
  • Flaxious
    Flaxious almost 7 years
    If you want to remove commas from numbers inside a string that also contains words use this pattern if you want to avoid removing commas in words: /[0-9]+[,]+[0-9]+/
  • Jacques Amar
    Jacques Amar over 5 years
    You have forgotten the possibility of negative numbers. You'll need to add the - $var = floatval(preg_replace('/[^\d\.\-]/', '', $var));
  • Nikhil Bhatia
    Nikhil Bhatia over 4 years
    Works! But the question is about removing commas from existing numeric strings and not to format it on any numeric value. If you try number_format('1,235.369',2,'.',''), it won't work.
  • mickmackusa
    mickmackusa about 4 years
    This answer is not elegant. Why would you all preg_match() then str_replace() when it can be done with preg_replace() alone?
  • mickmackusa
    mickmackusa about 4 years
    The dot and they hyphen do not need to be escaped in the character class.
  • mickmackusa
    mickmackusa about 4 years
    The OP has stated that $a = "1,435" is not numeric. So this answer will fail. This answer is also low value because there is no explanation of how it works or why it is a good idea.