How can I convert Persian/Arabic numbers to English numbers with a simple function ?
Persian/Arabic numbers:
۰ // -> 0
۱ // -> 1
۲ // -> 2
۳ // -> 3
۴ // -> 4
۵ // -> 5
۶ // -> 6
۷ // -> 7
۸ // -> 8
۹ // -> 9
numbers over the unicode :
$num0="۰";
$num1="۱";
$num2="۲";
$num3="۳";
$num4="۴";
$num5="۵";
$num6="۶";
$num7="۷";
$num8="۸";
$num9="۹";
Here’s a short function:
function convert($string) {
$persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
$arabic = ['٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١','٠'];
$num = range(0, 9);
$convertedPersianNums = str_replace($persian, $num, $string);
$englishNumbersOnly = str_replace($arabic, $num, $convertedPersianNums);
return $englishNumbersOnly;
}
You can use the unicode instead of the characters in $persian
(I think).
Answer:
I use this function. It’s convert both Persian and Arabic numbers to English:
function faTOen($string) {
return strtr($string, array('۰'=>'0', '۱'=>'1', '۲'=>'2', '۳'=>'3', '۴'=>'4', '۵'=>'5', '۶'=>'6', '۷'=>'7', '۸'=>'8', '۹'=>'9', '٠'=>'0', '١'=>'1', '٢'=>'2', '٣'=>'3', '٤'=>'4', '٥'=>'5', '٦'=>'6', '٧'=>'7', '٨'=>'8', '٩'=>'9'));
}
sample:
echo faTOen("۰۱۲۳۴۵۶۷۸۹٠١٢٣٤٥٦٧٨٩"); // 01234567890123456789
Also you can use same way to convert English to Persian:
function enToFa($string) {
return strtr($string, array('0'=>'۰','1'=>'۱','2'=>'۲','3'=>'۳','4'=>'۴','5'=>'۵','6'=>'۶','7'=>'۷','8'=>'۸','9'=>'۹'));
}
Or English to Arabic:
function enToAr($string) {
return strtr($string, array('0'=>'٠','1'=>'١','2'=>'٢','3'=>'٣','4'=>'٤','5'=>'٥','6'=>'٦','7'=>'٧','8'=>'٨','9'=>'٩'));
}
Answer:
Because of people at Persian & Arabic region maybe use each other language , this is the complete solution to convert both type .
function convert2english($string) {
$newNumbers = range(0, 9);
// 1. Persian HTML decimal
$persianDecimal = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
// 2. Arabic HTML decimal
$arabicDecimal = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
// 3. Arabic Numeric
$arabic = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
// 4. Persian Numeric
$persian = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
$string = str_replace($persianDecimal, $newNumbers, $string);
$string = str_replace($arabicDecimal, $newNumbers, $string);
$string = str_replace($arabic, $newNumbers, $string);
return str_replace($persian, $newNumbers, $string);
}
Answer:
this is better.
we have two type of digits in arabic and persian. we must change all.
function convert($string) {
$persinaDigits1= array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
$persinaDigits2= array('٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١', '٠');
$allPersianDigits=array_merge($persinaDigits1, $persinaDigits2);
$replaces = array('0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9');
return str_replace($allPersianDigits, $replaces , $string);
}
thanks @Palladium
Be Careful when copying the code! Check twice how the array is presented in your editor or your will be in trouble!
Answer:
$fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
echo numfmt_parse($fmt, "۵") . "\n";
// 5
Answer:
For convert all persian number to english format you can use this function:
function Convertnumber2english($srting) {
$srting = str_replace('۰', '0', $srting);
$srting = str_replace('۱', '1', $srting);
$srting = str_replace('۲', '2', $srting);
$srting = str_replace('۳', '3', $srting);
$srting = str_replace('۴', '4', $srting);
$srting = str_replace('۵', '5', $srting);
$srting = str_replace('۶', '6', $srting);
$srting = str_replace('۷', '7', $srting);
$srting = str_replace('۸', '8', $srting);
$srting = str_replace('۹', '9', $srting);
return $srting;
}
Answer:
Two useful functions:
First convert to English number:
function convert_to_en_number($string) {
$persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
$arabic = ['٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١','٠'];
$num = range(0, 9);
$convertedPersianNums = str_replace($persian, $num, $string);
$englishNumbersOnly = str_replace($arabic, $num, $convertedPersianNums);
return $englishNumbersOnly;
}
Second convert to Persian number:
function convert_to_fa_number($string) {
$num = range(0, 9);
$arabic = ['٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١','٠'];
$persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
$convertedEnglishNums = str_replace($num, $persian, $string);
$persianNumbersOnly = str_replace($arabic, $persian, $convertedEnglishNums);
return $persianNumbersOnly;
}
Answer:
Very easy way to convert Arabic numbers to English number digits using below function:
function ArtoEnNumeric($string) {
return strtr($string, array('۰'=>'0', '۱'=>'1', '۲'=>'2', '۳'=>'3', '۴'=>'4', '۵'=>'5', '۶'=>'6', '۷'=>'7', '۸'=>'8', '۹'=>'9', '٠'=>'0', '١'=>'1', '٢'=>'2', '٣'=>'3', '٤'=>'4', '٥'=>'5', '٦'=>'6', '٧'=>'7', '٨'=>'8', '٩'=>'9'));
}
echo ArtoEnNumeric('۶۰۶۳١۶۳٨');
Output = 60631638
Answer:
$sting= '٩٨٥٤٠٣٧٦٣٢١';
// search stings
$seachstrings = array("١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩", "٠");
// replace strings
$replacestrings= array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
// replace function
$result= str_replace($seachstrings , $replacestrings, $sting);
print_r($result);
Answer:
I know this is a six-year old question, but I just came across this and developed a generic solution and I’d like to share it here for future readers.
Basically I use the NumberFormatter to generate the numbers in any foreign language, then I replace the numbers in the input string with the ones from English.
It should work for any language, but I wrote mine for Arabic.
/**
* Replace Arabic numbers by English numbers in a string
*
* @param $value string A string containing Arabic numbers.
* @param $source_language string The locale to convert chars from.
*
* @return string The string with Arabic numbers replaced by English numbers
*/
function englishifyNumbers ($value, $source_language = 'ar') {
// Remove any direction overriding chars [LRO (U+202D)]
$value = trim($value);
// Create an Arabic number formatter to generate Arabic numbers
$fmt = new \NumberFormatter(
$source_language,
\NumberFormatter::PATTERN_DECIMAL
);
// Create an array of English numbers to use for replacement
$english_numbers = range(0, 9);
// Convert the English numbers to Arabic numbers using the formatter
$arabic_numbers = array_map(function ($n) use ($fmt) {
// Trim to remove direction overriding chars [PDF (U+202C)]
return trim($fmt->format($n));
}, $english_numbers);
// Replace the numbers and return the result
return str_replace($arabic_numbers, $english_numbers, $value);
}
Answer:
Probably a better answer using some kind of replace, but for coding it yourself, this would be better than what you have:
$len = strlen($arabic);
for ( $i = 0; $i < $len; ++$i )
{
switch( $arabic[$i] )
{
case '۰':
$english .= '0';
break;
case '۱':
$english .= '1';
break;
case '۲':
$english .= '2';
break;
// and so on
case '۹':
$english .= '9';
break;
default:
$english .= $arabic[$i];
}
}
That should do it.
Answer:
how about something like this…
$number = arbic_to_english("۱۳");
echo $number; // 13
function arbic_to_english($number) {
$english_number = 0;
$matches = array();
preg_match_all('/&#\d{4};/', $number, $matches);
if(!count($matches) || !count($matches[0])) {
throw new Exception('Invalid number');
}
$power = count($matches[0]) - 1;
foreach($matches[0] as $arbic_digit) {
$english_digit = preg_replace('/&#\d{2}(\d{2});/', '$1', $arbic_digit) - 76;
$english_number += $english_digit * pow(10, $power--);
}
return $english_number;
}
Answer:
No need to divide the input string.
private function convert($input)
{
$unicode = array('۰', '۱', '۲', '۳', '٤', '٥', '٦', '۷', '۸', '۹');
$english = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
$string = str_replace($unicode, $english , $input);
return $string;
}
Examples
convert("۱۲۳٤٥"); //output : 12345
convert("۱۲34٥"); //output : 12345
convert("12345"); //output : 12345
Answer:
the best way is to use NumberFormatter:
$formatter = \NumberFormatter::create('en', \NumberFormatter::DECIMAL);
echo $formatter->parse('۱۳۹۹'); // outputs: 1399
Tags: phpphp