From 5f232348d7806948dcf87bb156145d7540b044ed Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Tue, 9 Jan 2024 19:29:00 -0800 Subject: [PATCH] Advanced Value Binder False Positive Looking for Fractions Fix #3861. Strings ending in `/` were inappropriately identifed as fractions. Fix regexp accordingly. --- src/PhpSpreadsheet/Cell/AdvancedValueBinder.php | 4 ++-- tests/PhpSpreadsheetTests/Cell/AdvancedValueBinderTest.php | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/PhpSpreadsheet/Cell/AdvancedValueBinder.php b/src/PhpSpreadsheet/Cell/AdvancedValueBinder.php index 09c2f749d..53ea87b37 100644 --- a/src/PhpSpreadsheet/Cell/AdvancedValueBinder.php +++ b/src/PhpSpreadsheet/Cell/AdvancedValueBinder.php @@ -43,9 +43,9 @@ class AdvancedValueBinder extends DefaultValueBinder implements IValueBinder } // Check for fractions - if (preg_match('/^([+-]?)\s*(\d+)\s?\/\s*(\d+)$/', $value, $matches)) { + if (preg_match('~^([+-]?)\s*(\d+)\s*/\s*(\d+)$~', $value, $matches)) { return $this->setProperFraction($matches, $cell); - } elseif (preg_match('/^([+-]?)(\d*) +(\d*)\s?\/\s*(\d*)$/', $value, $matches)) { + } elseif (preg_match('~^([+-]?)(\d+)\s+(\d+)\s*/\s*(\d+)$~', $value, $matches)) { return $this->setImproperFraction($matches, $cell); } diff --git a/tests/PhpSpreadsheetTests/Cell/AdvancedValueBinderTest.php b/tests/PhpSpreadsheetTests/Cell/AdvancedValueBinderTest.php index 7183b4fa7..d3f0ad664 100644 --- a/tests/PhpSpreadsheetTests/Cell/AdvancedValueBinderTest.php +++ b/tests/PhpSpreadsheetTests/Cell/AdvancedValueBinderTest.php @@ -150,6 +150,12 @@ class AdvancedValueBinderTest extends TestCase ['1 16/20', 1.8], ['12 20/100', 12.2], ['-1 4/20', -1.2], + ['407 / ', '407 / '], + ['407 /', '407 /'], + ['407 3/', '407 3/'], + ['-407 /4', -101.75], + [' /', ' /'], + [' / ', ' / '], ]; }