phpunicode

convert Persian/Arabic numbers to English numbers


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="۹";

Solution

  • 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);
    }