代码之家  ›  专栏  ›  技术社区  ›  Fadly Dzil

php-使用preg_replace允许负十进制

  •  0
  • Fadly Dzil  · 技术社区  · 7 年前

    我在这样的变量中有输入掩码 200.000,54

    这是我的php代码

    <?php
    
    class MoneyHelper
    {
        public function getAmount($money)
        {
            $cleanString = preg_replace('/([^0-9\.,])/i', '', $money);
            $onlyNumbersString = preg_replace('/([^0-9])/i', '', $money);
    
            $separatorsCountToBeErased = strlen($cleanString) - strlen($onlyNumbersString) - 1;
    
            $stringWithCommaOrDot = preg_replace('/([,\.])/', '', $cleanString, $separatorsCountToBeErased);
            $removedThousandSeparator = preg_replace('/(\.|,)(?=[0-9]{3,}$)/', '',  $stringWithCommaOrDot);
    
            //return (float) str_replace(',', '.', $removedThousandSeparator);
    
            return [
              'cleanString' => $cleanString,
              'onlyNumbersString' => $onlyNumbersString,
              'separatorsCountToBeErased' => $separatorsCountToBeErased,
              'stringWithCommaOrDot' => $stringWithCommaOrDot,
              'removedThousandSeparator' => $removedThousandSeparator,
              'result' => (float) str_replace(',', '.', $removedThousandSeparator)
    
            ];
    
        }
    }
    
    
    $obj = new MoneyHelper;
    echo var_dump($obj->getAmount('200.000,54')) ;
    

    array (size=6)
     'cleanString' => string '200.000,54' (length=10)
     'onlyNumbersString' => string '20000054' (length=8)
     'separatorsCountToBeErased' => int 1
     'stringWithCommaOrDot' => string '200000,54' (length=9)
     'removedThousandSeparator' => string '200000,54' (length=9)
     'result' => float 200000.54
    

    比方说 - 200.000,54

    那么结果还是一样的,,

    array (size=6)
      'cleanString' => string '200.000,54' (length=10)
      'onlyNumbersString' => string '20000054' (length=8)
      'separatorsCountToBeErased' => int 1
      'stringWithCommaOrDot' => string '200000,54' (length=9)
      'removedThousandSeparator' => string '200000,54' (length=9)
      'result' => float 200000.54
    

    我怎样才能得到结果中的负数? 请告知。。。

    使现代化

    你还没有告诉我们你想要的确切产量

    我需要: 'result' => float -200000.54

    1 回复  |  直到 7 年前
        1
  •  1
  •   mickmackusa Tom Green    7 年前

    您只需要将否定符号添加到否定的字符类中。我可能还会做一些其他的调整。

    片段:( Full Demo

    $cleanString = preg_replace('/[^\d.,-]/', '', $money);
    $onlyNumbersString = preg_replace('/[^\d-]/', '', $money);
    
    $separatorsCountToBeErased = strlen($cleanString) - strlen($onlyNumbersString) - 1;
    
    $stringWithCommaOrDot = preg_replace('/[,.]/', '', $cleanString, $separatorsCountToBeErased);
    $removedThousandSeparator = preg_replace('/[.,](?=\d{3,}$)/', '',  $stringWithCommaOrDot);
    

    array(6) {
      ["cleanString"]=>
      string(11) "-200.000,54"
      ["onlyNumbersString"]=>
      string(9) "-20000054"
      ["separatorsCountToBeErased"]=>
      int(1)
      ["stringWithCommaOrDot"]=>
      string(10) "-200000,54"
      ["removedThousandSeparator"]=>
      string(10) "-200000,54"
      ["result"]=>
      float(-200000.54)
    }