Remove non-numeric characters (except periods and commas) from a string

前端 未结 5 1912
一整个雨季
一整个雨季 2020-11-30 23:57

If I have the following values:

 $var1 = AR3,373.31

 $var2 = 12.322,11T

How can I create a new variable and set it to a copy of the data t

相关标签:
5条回答
  • 2020-12-01 00:15

    I'm surprised there's been no mention of filter_var here for this being such an old question...

    PHP has a built in method of doing this using sanitization filters. Specifically, the one to use in this situation is FILTER_SANITIZE_NUMBER_FLOAT with the FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND flags. Like so:

    $numeric_filtered = filter_var("AR3,373.31", FILTER_SANITIZE_NUMBER_FLOAT,
        FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
    echo $numeric_filtered; // Will print "3,373.31"
    

    It might also be worthwhile to note that because it's built-in to PHP, it's slightly faster than using regex with PHP's current libraries (albeit literally in nanoseconds).

    0 讨论(0)
  • 2020-12-01 00:23

    Simplest way to truly remove all non-numeric characters:

    echo preg_replace('/\D/', '', $string);
    

    \D represents "any character that is not a decimal digit"

    http://php.net/manual/en/regexp.reference.escape.php

    0 讨论(0)
  • If letters are always in the beginning or at the end, you can simply just use trim...no regex needed

    $string = trim($string, "a..zA..Z"); // this also take care of lowercase
    
    "AR3,373.31" --> "3,373.31"
    "12.322,11T" --> "12.322,11"
    "12.322,11"  --> "12.322,11"
    
    0 讨论(0)
  • 2020-12-01 00:27

    You could use filter_var to remove all illegal characters except digits, dot and the comma.

    • The FILTER_SANITIZE_NUMBER_FLOAT filter is used to remove all non-numeric character from the string.
    • FILTER_FLAG_ALLOW_FRACTION is allowing fraction separator " . "
    • The purpose of FILTER_FLAG_ALLOW_THOUSAND to get comma from the string.

    Code

    $var1 = '12.322,11T';
    
    echo filter_var($var1, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
    

    Output

    12.322,11
    

    To read more about filter_var() and Sanitize filters

    0 讨论(0)
  • 2020-12-01 00:33

    You could use preg_replace to swap out all non-numeric characters and the comma and period/full stop as follows:

    $testString = '12.322,11T';
    echo preg_replace('/[^0-9,.]+/', '', $testString);
    

    The pattern can also be expressed as /[^\d,.]+/

    0 讨论(0)
提交回复
热议问题