php - regex - how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01)?

后端 未结 6 2100
清酒与你
清酒与你 2020-12-05 21:26

how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01) ? I have a regex but doesn\'t seem to play well with commas

preg_match(\'/([0-         


        
6条回答
  •  长情又很酷
    2020-12-05 21:43

    The locale-aware float (%f) might be used with sscanf.

    $result = sscanf($s, '%f')
    

    That doesn't split the parts into an array though. It simply parses a float.

    See also: http://php.net/manual/en/function.sprintf.php

    A regex approach:

    /([0-9]{1,3}(?:,[0-9]{3})*\.[0-9]+)/
    

提交回复
热议问题