Convert English numbers to Arabic numerals

好久不见. 提交于 2019-11-27 03:13:20

问题


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. I live in an Arab country, so please don't go around explaining to me what Arabic numerals look like. If you can help, good and well. I don't need bogus ideas.

http://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Arabic_numerals-en.svg/500px-Arabic_numerals-en.svg.png


回答1:


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



回答2:


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



回答3:


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.




回答4:


Are you trying to convert e.g. Sixty-nine to تسعة وستون? Try if you can call the Google Translate service or another translation service. Otherwise, you should write your own numeral parser and use your knowledge of the Arabic language to translate it to Arabic. (But using a translation service would be a lot easier.) (Then again, PHP might already have a function that writes out numbers and digits in the proper way.)




回答5:


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




回答6:


Simple solution with strtr:

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

To convert from Eastern Arabic to Arabic:

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


来源:https://stackoverflow.com/questions/3386835/convert-english-numbers-to-arabic-numerals

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!