21

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

apaderno
  • 28,547
  • 16
  • 75
  • 90
HyderA
  • 20,651
  • 42
  • 112
  • 180

7 Answers7

32

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);
Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • But will that take care of rtl nature of the arabic lanugage? for instance, will 123 show up as ٣٢١ – HyderA Aug 02 '10 at 11:11
  • @gAMBOOKa nope - you'd have to turn it around using `strrev()`: http://www.php.net/manual/en/function.strrev.php – Pekka Aug 02 '10 at 11:12
  • 4
    @WimtenBrink The East Arabic numerals are the following: `$east_arabic = array("٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩");` I think the ones you provided are from India. @Pekka, @gAMBOOKa Unlike "alphabetical text", numerals are not RTL in Arabic, so you don't need to turn them around. – pau.moreno Aug 06 '12 at 19:45
  • @Pekka웃, I've been working with both RTL and LTR since 2006, I removed the part about `str_rev()`, browsers handle things without any problems. – Nabil Kadimi Aug 16 '15 at 11:47
6

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);
}
Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58
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.

Mario Awad
  • 1,410
  • 17
  • 32
0

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

C_Rance
  • 661
  • 12
  • 25
0

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

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

Wim ten Brink
  • 25,901
  • 20
  • 83
  • 149
  • No, i need to convert 10 to it's arabic counterpart. I have no knowledge of the Arabic language. That's a stupid suggestion anyway. – HyderA Aug 02 '10 at 10:29
  • No, i mean 1, 2, 3, 4.... I found plenty of Roman numeral classes as well. I know google too! – HyderA Aug 02 '10 at 10:32
  • Then don't use "numerals". Numerals are the words that describe numbers. 54 is a number, fifty-four is a numeral. – Wim ten Brink Aug 02 '10 at 10:33
  • 7
    It's funny isn't it, the arabic number for 10 is 10. That's because we actually use arabic numbers throughout most of the world, and certainly in the world of mathematics – Mark Baker Aug 02 '10 at 10:34
  • 2
    No Mark, we use Arabic numbers, not numerals! :-) – Wim ten Brink Aug 02 '10 at 10:36
  • Numbers in arabic are exactly the same as in any other language(well.. in morden language anyway). In fact the, if i remember history correctly, the numbers we use today (1, 2, 3 etc) come from middle east in the first place. And check out some random saudi arabian newspaper: http://www.alriyadh.com/ notice something familiari there, perhaps? – Odif Yltsaeb Aug 02 '10 at 10:38
  • @Workshop Alex: *sigh* no, we use arabic numerals: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 –  Aug 02 '10 at 10:38
  • @Mark: http://upload.wikimedia.org/wikipedia/commons/thumb/d/d8/EgyptphoneKeypad.jpg/220px-EgyptphoneKeypad.jpg – HyderA Aug 02 '10 at 10:39
  • 1
    @hop, the definition of numeral is: "specific words in a natural language that represent numbers." Your example are just numbers. It's a BIG difference. Then again, a lot of people are confused by this. – Wim ten Brink Aug 02 '10 at 10:41
  • @Workshop Alex: you confuse linguistics and writing. there are no "arabic numbers" and "chinese numbers" and "english numbers" -- numbers are the same everywhere in the universe. –  Aug 02 '10 at 10:43
  • 1
    What @gAMBOOKa seems to be meaning is *eastern arabic/indic* numerals: http://en.wikipedia.org/wiki/Eastern_Arabic_numerals – Pekka Aug 02 '10 at 10:43
  • 1
    @gAMBOOKa **no**. Arabic numerals are `12345....` see http://en.wikipedia.org/wiki/Arabic_numerals – Pekka Aug 02 '10 at 10:45
  • 1
    @Pekka: Wikipedia is not always accurate http://en.wikipedia.org/wiki/Arabic_script#Numerals – HyderA Aug 02 '10 at 10:48
  • 1
    @hop, there are arabic and chinese numeral symbols. Technically, 0123456789 are also numeral symbols but these are so common worldwide that they are referred to as numbers. Or, to be more precise, those numbers are West Arabic numeral symbols. (Thus not even english numerals!) – Wim ten Brink Aug 02 '10 at 10:48
  • 1
    @gambooka `There are two kinds of numerals used in Arabic writing; standard numerals(predominant in the Arab World), and Eastern Arabic numerals (used in Iran, Afghanistan, Pakistan and India)` what is not accurate here? What you are looking for does not seem to be referred to as "Arabic Numeral"s in any case. – Pekka Aug 02 '10 at 10:50
  • @Workshop Alex: your worldview is narrow it's not even funny. in german those are commonly referred to as "Ziffern" (numerals) not "Zahlen" (numbers), so what's your point again? –  Aug 02 '10 at 11:01
  • 1
    I've been to Kuwait, Saudia and the UAE. And what you refer to as Eastern Arabic Numerals are predominant in these countries. You see them as phone numbers on shops, street numbers, highway speed limits, government documents, everywhere. – HyderA Aug 02 '10 at 11:03
  • 3
    @gAMBOOKa I'm pretty sure the global consensus is that "Arabic Numerals" are 1234567890. They are technically to be called "western arabic" to be exact, but they still are arabic. Anyway, you would just have had to give one full example of what you want - it would have prevented all this discussion. – Pekka Aug 02 '10 at 11:05
  • True! But interesting discussion anyway. – HyderA Aug 02 '10 at 11:11
  • @hop, the question wasn't about German and I'm not interested in any German linguistics. Besides, numeral would translate to "Zahlennamen", not just "Zahlen". And "Ziffer" would translate to "digit". And why do we continue to discuss something this trivial anyways? Read a dictionary! ;-) – Wim ten Brink Aug 02 '10 at 11:14
  • @gAMBOOKa yes, very interesting for me as well! I didn't know about eastern arabic numerals at all. Learned something. – Pekka Aug 02 '10 at 11:14
  • @Workshop Alex: i give up. it's obvious that you don't even _read_ what i _write_. –  Aug 02 '10 at 11:34
  • @hop, and you don't read what I write. You're confusing Numerals (zahlennamen) with numbers (zahlen) while there is a clear difference. (I fixed the confusion in the original Q, btw.) I talk about numerals, you talk about numbers. See the difference? – Wim ten Brink Aug 02 '10 at 11:58
-1

Simple solution with strtr:

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

To convert from Eastern Arabic to Arabic:

strtr($str, array_flip(['٠','١','٢','٣','٤','٥','٦','٧','٨','٩']));
mustafa.0x
  • 1,476
  • 2
  • 17
  • 30