From 8df4a95afa092e58fb29e587c5649b8471383f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Sousa?= Date: Wed, 24 Jan 2018 17:20:23 -0300 Subject: [PATCH 1/5] Strip whitespaces before preg cheks Fix a bug when trying to parse some Santander Bank ofx file --- lib/OfxParser/Ofx.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/OfxParser/Ofx.php b/lib/OfxParser/Ofx.php index ad494ed..317d31f 100644 --- a/lib/OfxParser/Ofx.php +++ b/lib/OfxParser/Ofx.php @@ -324,6 +324,8 @@ private function createDateTimeFromStr($dateString, $ignoreErrors = false) */ private function createAmountFromStr($amountString) { + $amountString = trim($amountString); + // Decimal mark style (UK/US): 000.00 or 0,000.00 if (preg_match('/^-?([\d,]+)(\.?)([\d]{2})$/', $amountString) === 1) { return (float)preg_replace( From 193371a2d1a687f0959c911e6883522037bc5cb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Sousa?= Date: Wed, 24 Jan 2018 17:39:27 -0300 Subject: [PATCH 2/5] Amount parse enhancement Credits to https://stackoverflow.com/a/17969215/3746290 --- lib/OfxParser/Ofx.php | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/lib/OfxParser/Ofx.php b/lib/OfxParser/Ofx.php index 317d31f..0b092e5 100644 --- a/lib/OfxParser/Ofx.php +++ b/lib/OfxParser/Ofx.php @@ -326,22 +326,8 @@ private function createAmountFromStr($amountString) { $amountString = trim($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 - ); - } - - // 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('/^(?.*)(?[\.,])(?[0-9]+)$/', $amountString, $matches) === 1) { + $amountString = preg_replace('/[^0-9\-]+/', '', $matches['integer']) . '.' . $matches['decimals']; } return (float)$amountString; From 83618f74ac92be603e7cc684b00c1367fd7a7ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Sousa?= Date: Wed, 24 Jan 2018 18:13:09 -0300 Subject: [PATCH 3/5] Refactoring --- lib/OfxParser/Ofx.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/OfxParser/Ofx.php b/lib/OfxParser/Ofx.php index 0b092e5..72fcb47 100644 --- a/lib/OfxParser/Ofx.php +++ b/lib/OfxParser/Ofx.php @@ -326,8 +326,8 @@ private function createAmountFromStr($amountString) { $amountString = trim($amountString); - if (preg_match('/^(?.*)(?[\.,])(?[0-9]+)$/', $amountString, $matches) === 1) { - $amountString = preg_replace('/[^0-9\-]+/', '', $matches['integer']) . '.' . $matches['decimals']; + if (preg_match('/^(?[-\+]?)(?.*)(?[\.,])(?[\d]+)$/', $amountString, $matches) === 1) { + $amountString = $matches['signal'] . preg_replace('/[^\d]+/', '', $matches['integer']) . '.' . $matches['decimals']; } return (float)$amountString; From 2909a5b10e61dbe9a1f5ee6652f8ebb433595f3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Sousa?= Date: Mon, 18 Sep 2023 13:00:21 -0300 Subject: [PATCH 4/5] Fix double utf8 encoding --- lib/OfxParser/Parser.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/OfxParser/Parser.php b/lib/OfxParser/Parser.php index 081c82a..37759ce 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, ''); From 85113660466fa4a25e187fe17691dd832a99bf4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Sousa?= Date: Thu, 18 Jul 2024 12:18:16 -0300 Subject: [PATCH 5/5] Update Parser.php --- lib/OfxParser/Parser.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/OfxParser/Parser.php b/lib/OfxParser/Parser.php index 37759ce..d38f58e 100644 --- a/lib/OfxParser/Parser.php +++ b/lib/OfxParser/Parser.php @@ -79,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()) {