diff --git a/lib/OfxParser/Ofx.php b/lib/OfxParser/Ofx.php index b93c075..72fcb47 100644 --- a/lib/OfxParser/Ofx.php +++ b/lib/OfxParser/Ofx.php @@ -324,22 +324,10 @@ private function createDateTimeFromStr($dateString, $ignoreErrors = false) */ private 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 = trim($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 - ); + if (preg_match('/^(?[-\+]?)(?.*)(?[\.,])(?[\d]+)$/', $amountString, $matches) === 1) { + $amountString = $matches['signal'] . preg_replace('/[^\d]+/', '', $matches['integer']) . '.' . $matches['decimals']; } return (float)$amountString; diff --git a/lib/OfxParser/Parser.php b/lib/OfxParser/Parser.php index 081c82a..d38f58e 100644 --- a/lib/OfxParser/Parser.php +++ b/lib/OfxParser/Parser.php @@ -38,7 +38,9 @@ public function loadFromFile($ofxFile) */ public function loadFromString($ofxContent) { - $ofxContent = utf8_encode($ofxContent); + if (mb_detect_encoding($ofxContent, 'UTF-8', true) !== 'UTF-8') { + $ofxContent = utf8_encode($ofxContent); + } $ofxContent = $this->conditionallyAddNewlines($ofxContent); $sgmlStart = stripos($ofxContent, ''); @@ -77,6 +79,8 @@ private function xmlLoadString($xmlString) { libxml_clear_errors(); libxml_use_internal_errors(true); + // Replace unescaped ampersands. @see https://www.php.net/manual/en/simplexml.examples-errors.php#115708 + $xmlString = preg_replace('/&(?!;{6})/', '&', $xmlString); $xml = simplexml_load_string($xmlString); if ($errors = libxml_get_errors()) {