Convert English numbers to Arabic numerals
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.
ht开发者_开发技巧tp://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Arabic_numerals-en.svg/500px-Arabic_numerals-en.svg.png
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);
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);
}
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.
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
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;
}
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.)
Simple solution with strtr:
strtr($str, ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩']);
To convert from Eastern Arabic to Arabic:
strtr($str, array_flip(['٠','١','٢','٣','٤','٥','٦','٧','٨','٩']));
精彩评论