note 80281 deleted from function.number-format by cmb
| From: | cmb@php.net | Date: | Sun, 13 Jan 2019 10:45:36 +0000 |
| Subject: | note 80281 deleted from function.number-format by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210054@lists.php.net to get a copy of this message | ||
Note Submitter: www.produkte24.com
----
I have to verify price data from several formats, eg:
- 12.345,67 (German)
- 12,345.67 (English)
- 12 345,67 (French)
- 12'345,67 (Swiss)
Here is a quick function which helps me out of all this format mess, but please not the I only check
for:
- positive values
- max 2 digits on the right side (.12)
function mk_price_to_float($price){
$price = trim($price);
if(preg_match("~^([0-9]+|(?:(?:[0-9]{1,3}([.,'
]))+[0-9]{3})+)(([.,])[0-9]{1,2})?$~", $price, $r)){
if(!empty($r['2'])){
$pre = preg_replace("~[".$r['2']."]~", "",
$r['1']);
}else{
$pre = $r['1'];
}
if(!empty($r['4'])){
$post = ".".preg_replace("~[".$r['4']."]~", "",
$r['3']);
}else{
$post = false;
}
$form_price = $pre.$post;
return $form_price;
}
return false;
}
This code is used at http://www.produkte24.com/ and http://www.who-sells-it.com/ and works like a charm.