Skip to content

Commit

Permalink
Ajuste de regex para formatação de valor
Browse files Browse the repository at this point in the history
  • Loading branch information
cassioperin-bling committed Feb 26, 2021
1 parent 262a9ee commit 77b950f
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions lib/OfxParser/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,16 @@ public static function createDateTimeFromStr($dateString, $ignoreErrors = false)
*/
public static function createAmountFromStr($amountString)
{
// Decimal mark style (UK/US): 000.00 or 0,000.00
if (preg_match('/^(-|\+)?([\d,]+)(\.?)([\d]{2})$/', $amountString) === 1) {
return (float)preg_replace(
['/([,]+)/', '/\.?([\d]{2})$/'],
['', '.$1'],
$amountString
);
$amountString = str_replace(' ', '', $amountString);

// US style: [.0 | .00 | 000.00 | 0,000.00 | 0,000 | 0,000.0 | 000. | 0 | .]
if (preg_match('/^(-|\+)?(([\d]+)?|([\d]{1,3}((\,[\d]{3})+)?))(\.[\d]{0,2})?$/', $amountString) === 1) {
return (float)(str_replace(',', '', $amountString));
}

// European style: 000,00 or 0.000,00
if (preg_match('/^(-|\+)?([\d\.]+,?[\d]{2})$/', $amountString) === 1) {
return (float)preg_replace(
['/([\.]+)/', '/,?([\d]{2})$/'],
['', '.$1'],
$amountString
);
// BR style: [,0 | ,00 | 000,00 | 0.000,00 | 0.000 | 0.000,0 | 000, | 0 | ,]
if (preg_match('/^(-|\+)?(([\d]+)?|([\d]{1,3}((\.[\d]{3})+)?))(\,[\d]{0,2})?$/', $amountString) === 1) {
return (float)(str_replace(',', '.', (str_replace('.', '', $amountString))));
}

return (float)$amountString;
Expand Down

0 comments on commit 77b950f

Please sign in to comment.