To meet your requirement of formatting numbers by grouping digits in sets of three (e.g., 123,456,789) and also supporting Persian and Arabic numerals, we can extend the original function to first convert non-English digits to their English equivalents before applying the formatting logic.
✅ Key Features of the Enhanced Function:
-
Support for Persian and Arabic Digits
- Persian digits:
۰ ۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹
- Arabic digits:
٠ ١ ٢ ٣ ٤ ٥ ٦ ٧ ٨ ٩
These are converted to 0 1 2 3 4 5 6 7 8 9 respectively.
-
Formatting
- Reverses the string to insert commas every 3 digits.
- Reverses the string back to the correct order.
-
Flexible Input
- Accepts both numeric values and strings (e.g.,
'١٢٣٤٥٦٧٨٩' or 123456789).
✅ PHP Implementation
<?php
/**
* Converts Persian and Arabic digits to English digits.
*
* @param string $str Input string that may contain Persian or Arabic digits.
* @return string String with all digits converted to English.
*/
function convertArabicPersianToEnglish($str) {
// Persian digits
$persianDigits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
// Arabic digits
$arabicDigits = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
// English digits
$englishDigits = range('0', '9');
// Build replacement map
$replaceMap = [];
foreach ($persianDigits as $i => $char) {
$replaceMap[$char] = $englishDigits[$i];
}
foreach ($arabicDigits as $i => $char) {
$replaceMap[$char] = $englishDigits[$i];
}
// Replace digits using the map
return strtr($str, $replaceMap);
}
/**
* Formats a number by grouping digits in sets of 3 from the right.
* Also converts Persian/Arabic digits to English first.
*
* @param mixed $number Input number as a string or integer.
* @return string Formatted number with commas.
*/
function formatAndReverseNumber($number) {
// Convert to string
$numberStr = (string)$number;
// Convert Persian and Arabic digits to English
$convertedStr = convertArabicPersianToEnglish($numberStr);
// Reverse the string to insert commas from the right
$reversed = strrev($convertedStr);
// Insert commas every 3 digits (except the last group)
$reversedWithCommas = preg_replace('/(\d{3})(?=\d)/', '$1,', $reversed);
// Reverse back to the correct order
return strrev($reversedWithCommas);
}
// Example usage:
echo formatAndReverseNumber('۱۲۳۴۵۶۷۸۹') . "\n"; // Output: 123,456,789
echo formatAndReverseNumber('١٢٣٤٥٦٧٨٩') . "\n"; // Output: 123,456,789
echo formatAndReverseNumber(123456789) . "\n"; // Output: 123,456,789
echo formatAndReverseNumber('١٢') . "\n"; // Output: 12
echo formatAndReverseNumber('١٢٣٤') . "\n"; // Output: 1,234
?>
✅ How It Works
-
Digit Conversion
- The helper function
convertArabicPersianToEnglish maps Persian and Arabic digits to their English counterparts using strtr.
- This ensures that even if the input is in a non-English numeral system, it is first converted into a standard English number string.
-
Reversal and Formatting
- The string is reversed so that commas can be inserted every 3 digits starting from the right.
- A regular expression is used to match every group of 3 digits that is followed by another digit, and a comma is inserted.
- The string is reversed back to restore the original order, now with commas properly placed.
✅ Benefits
- Internationalization Ready: Handles Persian and Arabic numerals seamlessly.
- Robust: Works with both numeric and string inputs.
- Maintainable: Separation of digit conversion and formatting logic makes the code easier to extend or debug.
📌 Note
This implementation assumes that the input is a valid number string. If the input may contain non-numeric characters (e.g., currency symbols, letters), additional validation or sanitization may be required.