convert Persian/Arabic numbers to English numbers

Viewed 58705

How can I convert Persian/Arabic numbers to English numbers with a simple function ?

Persian/Arabic numbers:

۰   //  -> 0
۱   //  -> 1
۲   //  -> 2
۳   //  -> 3
۴   //  -> 4
۵   //  -> 5
۶   //  -> 6
۷   //  -> 7
۸   //  -> 8
۹   //  -> 9

numbers over the unicode :

$num0="۰";
$num1="۱";
$num2="۲";
$num3="۳";
$num4="۴";
$num5="۵";
$num6="۶";
$num7="۷";
$num8="۸";
$num9="۹";
17 Answers

Because people in the Persian & Arabic region maybe use each other keyboard types, this is the complete solution to convert both types.

Based on @palladium answer.

function convert2english($string) {
    $newNumbers = range(0, 9);
    // 1. Persian HTML decimal
    $persianDecimal = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
    // 2. Arabic HTML decimal
    $arabicDecimal = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
    // 3. Arabic Numeric
    $arabic = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
    // 4. Persian Numeric
    $persian = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');

    $string =  str_replace($persianDecimal, $newNumbers, $string);
    $string =  str_replace($arabicDecimal, $newNumbers, $string);
    $string =  str_replace($arabic, $newNumbers, $string);
    return str_replace($persian, $newNumbers, $string);
}

Two useful functions:

First convert to English number:

function convert_to_en_number($string) {
    $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
    $arabic = ['٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١','٠'];

    $num = range(0, 9);
    $convertedPersianNums = str_replace($persian, $num, $string);
    $englishNumbersOnly = str_replace($arabic, $num, $convertedPersianNums);

    return $englishNumbersOnly;
}

Second convert to Persian number:

function convert_to_fa_number($string) {
    $num = range(0, 9);
    $arabic = ['٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١','٠'];
    $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

    $convertedEnglishNums = str_replace($num, $persian, $string);
    $persianNumbersOnly = str_replace($arabic, $persian, $convertedEnglishNums);

    return $persianNumbersOnly;
}

Very easy way to convert Arabic numbers to English number digits using below function:

function ArtoEnNumeric($string) {
    return strtr($string, array('۰'=>'0', '۱'=>'1', '۲'=>'2', '۳'=>'3', '۴'=>'4', '۵'=>'5', '۶'=>'6', '۷'=>'7', '۸'=>'8', '۹'=>'9', '٠'=>'0', '١'=>'1', '٢'=>'2', '٣'=>'3', '٤'=>'4', '٥'=>'5', '٦'=>'6', '٧'=>'7', '٨'=>'8', '٩'=>'9'));
}

echo ArtoEnNumeric('۶۰۶۳١۶۳٨'); 
Output = 60631638

I know this is a six-year old question, but I just came across this and developed a generic solution and I'd like to share it here for future readers.

Basically I use the NumberFormatter to generate the numbers in any foreign language, then I replace the numbers in the input string with the ones from English.

It should work for any language, but I wrote mine for Arabic.

/**
 * Replace Arabic numbers by English numbers in a string
 *
 * @param $value string A string containing Arabic numbers.
 * @param $source_language string The locale to convert chars from.
 *
 * @return string The string with Arabic numbers replaced by English numbers
 */
function englishifyNumbers ($value, $source_language = 'ar') {
    // Remove any direction overriding chars [LRO (U+202D)]
    $value = trim($value);

    // Create an Arabic number formatter to generate Arabic numbers
    $fmt = new \NumberFormatter(
        $source_language, 
        \NumberFormatter::PATTERN_DECIMAL
    );

    // Create an array of English numbers to use for replacement
    $english_numbers = range(0, 9);

    // Convert the English numbers to Arabic numbers using the formatter
    $arabic_numbers = array_map(function ($n) use ($fmt) {
        // Trim to remove direction overriding chars [PDF (U+202C)]
        return trim($fmt->format($n));

    }, $english_numbers);

    // Replace the numbers and return the result
    return str_replace($arabic_numbers, $english_numbers, $value);
}
$sting= '٩٨٥٤٠٣٧٦٣٢١'; 

// search stings
$seachstrings = array("١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩", "٠"); 

// replace strings 
$replacestrings= array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0"); 

// replace function 
$result= str_replace($seachstrings , $replacestrings, $sting); 

print_r($result); 

the best way is to use NumberFormatter:

$formatter = \NumberFormatter::create('en', \NumberFormatter::DECIMAL);
echo $formatter->parse('۱۳۹۹'); // outputs: 1399

Here is a function that takes a string and zero of source language and zero of destination language

In below example I am converting numbers from Hindi to Latin for a given string

val = LangNum2Num("जिनसे आप कुछ ९० सीख सकते ३४ हैं हम लाये हैं ७ आपके लिये कुछ", "०", 0)
console.log(val)
// Prints जिनसे आप कुछ 90 सीख सकते 34 हैं हम लाये हैं 7 आपके लिये कुछ




// Coverts numbers insides a string to other language number
// str is the string
// zeroFrom is the zero number from the language you want to convert
// zeroTo is the zero number of the languague you want to convert to

function LangNum2Num(str, zeroFrom, zeroTo) {
  // Get the UTF-16 code points of zeros
  codezeroFrom = ("" + zeroFrom).codePointAt(0)
  codezeroTo = ("" + zeroTo).codePointAt(0)

  // Make array containing codepoints from 0 to 10 for the language
  var FromArr = [...Array(10).keys()].map(e => codezeroFrom + e)
  var ToArr = [...Array(10).keys()].map(e => codezeroTo + e)

  // Split the string into array, if we catch a number, we will return number from destination language
  return str.split('').map(e => FromArr.includes(e.codePointAt(0)) ? String.fromCodePoint(ToArr[FromArr.indexOf(e.codePointAt(0))]) : e).join('')

}

I used this function

function toEngNumbers($string)
{
    $arabic = ['٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١', '٠'];
    $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
    $english = ['9', '8', '7', '6', '5', '4', '3', '2', '1', '0'];

    $convertedPersianNums = str_replace($persian, $english, $string);
    $converted = str_replace($arabic, $english, $convertedPersianNums);
    return $converted;
}

Based on @Root answer:

function to_english_number( $string ) {

    $persianDecimal = [ '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹' ];
    $arabicDecimal  = [ '٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩' ];

    $persian = [ '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹' ];
    $arabic  = [ '٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١', '٠' ];

    $num = range( 0, 9 );

    $string = str_replace( $persianDecimal, $num, $string );
    $string = str_replace( $arabicDecimal, $num, $string );

    $string = str_replace( $persian, $num, $string );
    $string = str_replace( $arabic, $num, $string );

    return $string;
}
Related