Convert Persian/Farsi and Arabic numbers to English numbers

Convert Persian/Farsi and Arabic numbers to English numbers

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Converting Persian and Arabic Numerals to English in Code

Handling numeric input from non-Latin scripts is a common requirement when building applications for international markets. Users often type or paste data using Persian (Farsi) or Arabic-Indic digits, which visually resemble standard ASCII numbers but possess distinct Unicode code points. If your application relies on strict integer comparisons, mathematical calculations, or database sorting without proper normalization, these characters can cause significant logic errors.

The most straightforward approach is to replace the specific Unicode characters with their ASCII equivalents. This involves mapping each Persian digit (Û° through Û¹) to its standard English counterpart (0 through 9). While some developers might rely on complex regex patterns or third-party localization libraries, a direct character substitution table offers predictable performance and zero external dependencies.

Here is the mapping for the standard Persian digits:

// Mapping for Persian/Arabic-Indic digits
$persianToEnglish = [
    'Û°' => '0',
    'Û±' => '1',
    'Û²' => '2',
    'Û³' => '3',
    'Û´' => '4',
    'Ûµ' => '5',
    'Û¶' => '6',
    'Û·' => '7',
    'Û¸' => '8',
    'Û¹' => '9',
];

function convertPersianToEnglish(string $input): string {
    foreach ($persianToEnglish as $persian => $english) {
        $input = str_replace($persian, $english, $input);
    }
    return $input;
}

This function iterates through the mapping array and performs a str_replace for each character. It is efficient enough for typical input lengths and handles mixed content gracefully. For example, a string like "Û±Û´Û°Û²" becomes "1402", while "Hello Ûµ World" correctly transforms to "Hello 5 World".

However, you must be aware of the Unicode complexity surrounding these characters. The source material highlights two distinct sets of digits often conflated under the term "Arabic numbers."

The first set is the Eastern Arabic-Indic digits (often used in Persian and Urdu contexts), which correspond to the code points ۰ through ۹. These are the characters shown above. The second set, frequently encountered in standard Arabic text, uses different code points (٠ through ٩). While both sets represent the same sequence of numbers (0–9), they occupy completely different locations in the Unicode table.

If your application needs to support standard Arabic numerals alongside Persian ones, your conversion logic must account for both ranges. Failing to do so results in partial conversions where some digits remain as non-ASCII characters after processing. A robust implementation often consolidates these mappings into a single associative array or uses strtr with a combined replacement string for better performance over multiple replacements.

// Handling both Persian (Eastern) and Standard Arabic digits
$fullMapping = [
    // Eastern Arabic-Indic (Persian/Farsi style)
    'Û°' => '0', 'Û±' => '1', 'Û²' => '2', 'Û³' => '3', 
    'Û´' => '4', 'Ûµ' => '5', 'Û¶' => '6', 'Û·' => '7', 
    'Û¸' => '8', 'Û¹' => '9',
    
    // Standard Arabic-Indic
    'Ù ' => '0', 'Ù¡' => '1', 'Ù¢' => '2', 'Ù£' => '3', 
    'Ù¤' => '4', 'Ù¥' => '5', 'Ù¦' => '6', 'Ù§' => '7', 
    'Ù¨' => '8', 'Ù©' => '9',
];

function normalizeNumbers(string $input): string {
    return strtr($input, $fullMapping);
}

Using strtr is generally preferred over a loop for this specific task because it handles all replacements in a single pass, reducing function call overhead.

Trade-offs and Pitfalls

When implementing number normalization, it is crucial to consider where the conversion happens relative to other operations. If you are storing data in a database, normalizing at the application layer before saving ensures consistency regardless of how the user inputs the data. However, if you normalize too early in a pipeline involving text processing (like NLP or search indexing), you might inadvertently alter semantic meaning or break tokenizers that expect raw Unicode characters.

Additionally, be cautious with floating-point numbers. Converting the string representation is straightforward, but ensure you do not strip decimal points or other formatting characters if they are encoded differently in the source script. The conversion logic should strictly target the digit characters themselves.

For larger projects or high-traffic APIs, relying on a static array might seem trivial, but it ensures your solution remains under your control without waiting for external library updates. If you need to handle thousands of languages with similar numeric systems, libraries like symfony/poly-intl (PHP) or the Intl.NumberFormat API in JavaScript provide more sophisticated, though heavier, normalization utilities.

For further guidance on handling internationalization and data normalization within PHP projects, resources from https://laravelcompany.com can offer insights into best practices for building robust, locale-aware applications.

Our network