1

考虑以下值:

$format = ',0';        // Thousand separators ON, no decimal places 
$format = '0';         // Thousand separators OFF, no decimal places
$format = '0.000';     // Thousand separators OFF, 3 decimal places
$format = ',0.0';      // Thousand separators ON, 1 decimal place
  1. 首先要做的是查看是否$format以','为前缀。这告诉我启用了千位分隔符。
  2. 其次,我必须查看第一个零之后有多少个零。例如,以下将是小数点后 2 位“0.00”等。

我已经设法匹配表达式(这不是很难),但我想做的是提取单个匹配项,这样我就可以知道是否找到了“,”以及有多少个零等。 ..

这是我到目前为止所拥有的:

preg_match_all('/^\,?[0]?[\.]?([0])+?$/',$value['Field_Format'],$matches);
4

1 回答 1

1

我会使用不同的正则表达式并将子结果放入命名组:

if (preg_match(
    '/^
    (?P<thousands>,)? # Optional thousands separator
    0                 # Mandatory 0
    (?:               # Optional group:
     (?P<decimal>\.)  # Decimal separator
     (?P<digits>0+)   # followed by one or more zeroes
    )?                # (optional)
    $                 # End of string/x', 
    $subject, $regs)) {
    $thousands = $regs['thousands'];
    $decimal = $regs['decimal'];
    $digits = $regs['digits'];
}
于 2013-05-07T10:46:47.123 回答