简体   繁体   中英

Remove number formatting from a string

The following code will turn 1000 into 1,000:

$price = '1000';
$price2 = number_format($price);
echo $price2;

How would I turn 1,000 into 1000? I'm guessing it would be something along the lines of this:

$price = '1,000';
$price2 = remove_format($price);
echo $price2;

It's something that is very simple, but I'll make it into a function for you.

You can do this by just using something like the str_replace() function to remove the , this is what I came up with:

Code:

function remove_format($text){
    $text = str_replace(",", "", $text)
    return $text;
}
filter_var('1,000', FILTER_SANITIZE_NUMBER_INT)

I haven't tested it, but I'd use something like this:

    $a = "1,000";
    $b = str_replace( ',', '', $a );
    if( is_numeric( $b ) ) {
        $a = $b;
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM