Convert English numbers to Arabic numerals

前端 未结 7 675
余生分开走
余生分开走 2020-12-10 01:58

How can i convert English numbers being retrieved from the database to Arabic numeral symbols in PHP.

EDIT: Here\'re some examples of what Arabic numerals look like.

相关标签:
7条回答
  • 2020-12-10 02:08

    I would suggest u to try using resource file for different language especially if the contents are known. Example: 2 file language files, 1 named language_EN, another language_AR where langauge_EN is to store the values in english, and language_AR for arabic.

    so for instance u want store the number "1"

    in langauge_EN, do something like this. number.one=1

    then in langauge_AR, (pretend x is the number "1" in arabic, but I guess unicode is preferred) number.one=x

    so after u retrieve from database and knowing that it is number, your raw result comes out to be "one".

    so use number.+[result from database] to load from the langauage_AR This method is quite widely used in webpages which allow toggling of language. So at any time u need to convert back to english, just change back to language_EN. This works not only for number. Gd Luck

    0 讨论(0)
  • 2020-12-10 02:12

    Simple solution with strtr:

    strtr($str, ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩']);
    

    To convert from Eastern Arabic to Arabic:

    strtr($str, array_flip(['٠','١','٢','٣','٤','٥','٦','٧','٨','٩']));
    
    0 讨论(0)
  • 2020-12-10 02:21

    If you are referring to what Wikipedia calls eastern arabic / indic numerals, a simple replace operation should do.

    $western_arabic = array('0','1','2','3','4','5','6','7','8','9');
    $eastern_arabic = array('٠','١','٢','٣','٤','٥','٦','٧','٨','٩');
    
    $str = str_replace($western_arabic, $eastern_arabic, $str);
    
    0 讨论(0)
  • 2020-12-10 02:21

    Arabic Numbers:

    function to_arabic_number($number)
    {
        $number = str_replace("1","۱",$number);
        $number = str_replace("2","۲",$number);
        $number = str_replace("3","۳",$number);
        $number = str_replace("4","٤",$number);
        $number = str_replace("5","٥",$number);
        $number = str_replace("6","٦",$number);
        $number = str_replace("7","۷",$number);
        $number = str_replace("8","۸",$number);
        $number = str_replace("9","۹",$number);
        $number = str_replace("0","۰",$number);
        return $number;
    }
    

    Persian Numbers:

    function to_persian_number($number)
    {
        $number = str_replace("1","۱",$number);
        $number = str_replace("2","۲",$number);
        $number = str_replace("3","۳",$number);
        $number = str_replace("4","۴",$number);
        $number = str_replace("5","۵",$number);
        $number = str_replace("6","۶",$number);
        $number = str_replace("7","۷",$number);
        $number = str_replace("8","۸",$number);
        $number = str_replace("9","۹",$number);
        $number = str_replace("0","۰",$number);
        return $number;
    }
    
    0 讨论(0)
  • 2020-12-10 02:23

    Definitions

    • Western arabic numerals: "1234567890".
    • Eastern arabic numerals: "١٢٣٤٥٦٧٨٩٠".

    The answer

    I wrote a couple of functions (gist.github.com) a while back.

    Demo (3v4l.org)

        echo arabic_w2e("1234567890"); // Outputs: ١٢٣٤٥٦٧٨٩٠
        echo arabic_e2w("١٢٣٤٥٦٧٨٩٠"); // Outputs: 1234567890
    

    Code

    <?php
    
    /**
     * Converts numbers in string from western to eastern Arabic numerals.
     *
     * @param  string $str Arbitrary text
     * @return string Text with western Arabic numerals converted into eastern Arabic numerals.
     */
    function arabic_w2e($str)
    {
        $arabic_eastern = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
        $arabic_western = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        return str_replace($arabic_western, $arabic_eastern, $str);
    }
    
    /**
     * Converts numbers from eastern to western Arabic numerals.
     *
     * @param  string $str Arbitrary text
     * @return string Text with eastern Arabic numerals converted into western Arabic numerals.
     */
    function arabic_e2w($str)
    {
        $arabic_eastern = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
        $arabic_western = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        return str_replace($arabic_eastern, $arabic_western, $str);
    }
    
    0 讨论(0)
  • 2020-12-10 02:23

    Or you can just download and use this class from: http://www.phpclasses.org/package/6626-PHP-Convert-numbers-to-Arabic-representation.html (Tested and working). Cheers.

    0 讨论(0)
提交回复
热议问题