Convert English numbers to Arabic numerals

2019-01-18 10:49发布

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

标签: php arabic
6条回答
姐就是有狂的资本
2楼-- · 2019-01-18 11:20

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);
查看更多
做个烂人
3楼-- · 2019-01-18 11:27

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

查看更多
Anthone
4楼-- · 2019-01-18 11:29

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

查看更多
对你真心纯属浪费
5楼-- · 2019-01-18 11:37

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);
}
查看更多
迷人小祖宗
6楼-- · 2019-01-18 11:41

Simple solution with strtr:

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

To convert from Eastern Arabic to Arabic:

strtr($str, array_flip(['٠','١','٢','٣','٤','٥','٦','٧','٨','٩']));
查看更多
Lonely孤独者°
7楼-- · 2019-01-18 11:42

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.

查看更多
登录 后发表回答