diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index 11035b0b9827..2006e59b6346 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -439,4 +439,15 @@ protected static function ensureIntlExtensionIsInstalled() throw new RuntimeException('The "intl" PHP extension is required to use the ['.$method.'] method.'); } } + + /** + * Converting Persian/Arabic digits to English + * @param mixed $value + * @return bool|float|int + */ + public static function toEnglishFormat($value) + { + $numberFormatter = new NumberFormatter('ar-u-nu-latn', NumberFormatter::DECIMAL); + return $numberFormatter->parse($value); + } } diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index 0b07595e7ef2..b620423a5e9b 100644 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -523,3 +523,16 @@ function with($value, ?callable $callback = null) return is_null($callback) ? $value : $callback($value); } } + +if (! function_exists('to_english_numbers')) { + /** + * Converting Persian/Arabic digits to English + * @param mixed $value + * @return string + */ + function to_english_numbers($value): string + { + return Number::toEnglishFormat($value); + } +} + diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index aad594dfc719..f98c54d6afe9 100644 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -1572,6 +1572,13 @@ public function testPregReplaceArray($pattern, $replacements, $subject, $expecte preg_replace_array($pattern, $replacements, $subject) ); } + + public function testToEnglishNumbersConvertsPersianAndArabicDigits() + { + $this->assertEquals('123', to_english_numbers('۱۲۳')); + $this->assertEquals('456', to_english_numbers('٤٥٦')); + } + } trait SupportTestTraitOne diff --git a/tests/Support/SupportNumberTest.php b/tests/Support/SupportNumberTest.php index 7f7de7f3f1a2..a22c3155aa91 100644 --- a/tests/Support/SupportNumberTest.php +++ b/tests/Support/SupportNumberTest.php @@ -370,4 +370,11 @@ public function testParseFloat() $this->assertSame(1234.56, Number::parseFloat('1.234,56', locale: 'de')); $this->assertSame(1234.56, Number::parseFloat('1 234,56', locale: 'fr')); } + + #[RequiresPhpExtension('intl')] + public function testToEnglishFormat() + { + $this->assertEquals('123', Number::toEnglishFormat('۱۲۳')); + $this->assertEquals('456', Number::toEnglishFormat('٤٥٦')); + } }