Convert Persian/Arabic Numbers to English Numbers

convert Persian/Arabic numbers to English numbers

Because people in the Persian & Arabic region maybe use each other keyboard types, this is the complete solution to convert both types.

Based on @palladium answer.

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

How to convert Persian/Arabic numbers to English numbers in Kotlin?

Finally bellow function solved my problem:

fun PersianToEnglish(persianStr: String):String {
var result = ""
var en = '0'
for (ch in persianStr) {
en = ch
when (ch) {
'۰' -> en = '0'
'۱' -> en = '1'
'۲' -> en = '2'
'۳' -> en = '3'
'۴' -> en = '4'
'۵' -> en = '5'
'۶' -> en = '6'
'۷' -> en = '7'
'۸' -> en = '8'
'۹' -> en = '9'
}
result = "${result}$en"
}
return result
}

How to convert Persian and Arabic digits of a string to English using JavaScript?

Use this simple function to convert your string

var
persianNumbers = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g],
arabicNumbers = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g],
fixNumbers = function (str)
{
if(typeof str === 'string')
{
for(var i=0; i<10; i++)
{
str = str.replace(persianNumbers[i], i).replace(arabicNumbers[i], i);
}
}
return str;
};

Be careful, in this code the persian numbers codepage are different with the arabian numbers.

Example

var mystr = 'Sample text ۱۱۱۵۱ and ٢٨٢٢';
mystr = fixNumbers(mystr);

Refrence

Convert Persian to English numbers issue

$arr2 seems to contain Persian digits and $arr1 seems to contain Arabic digits. It means ٠ in the first array isn't the same ۰ in the second array, although shapes are similar.

To ensure they are different you could run bin2hex():

foreach ($arr2 as $key => $value) {
echo $key, ': ', bin2hex($value), ' - ', bin2hex($arr1[$key]), "\n";
}

Outputs:

0: dbb0 - d9a0
1: dbb1 - d9a1
2: dbb2 - d9a2
3: dbb3 - d9a3
4: dbb4 - d9a4
5: dbb5 - d9a5
6: dbb6 - d9a6
7: dbb7 - d9a7
8: dbb8 - d9a8
9: dbb9 - d9a9

convert english number with farsi or arabic number in Dart

Example:

String replaceFarsiNumber(String input) {
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const farsi = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

for (int i = 0; i < english.length; i++) {
input = input.replaceAll(english[i], farsi[i]);
}

return input;
}

main() {
print(replaceFarsiNumber('0-1-2-3-4-5-6-7-8-9')); // ==> ۰-۱-۲-۳-۴-۵-۶-۷-۸-۹
}

Convert arabic number to english number & the reverse in Dart

Arabic to English

String replaceArabicNumber(String input) {
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const arabic = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];

for (int i = 0; i < english.length; i++) {
input = input.replaceAll(arabic[i], english[i]);
}
print("$input");
return input;
}

to do the opposite replacing the English numbers with the Arabic ones(English to Arabic)

 String replaceEnglishNumber(String input) {
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const arabic = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];

for (int i = 0; i < english.length; i++) {
input = input.replaceAll(english[i], arabic[i]);
}
print("$input");
return input;
}


Related Topics



Leave a reply



Submit