Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert Persian/Arabic numbers to English numbers

Tags:

php

unicode

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="۹"; 
like image 492
Root Avatar asked Aug 01 '12 20:08

Root


1 Answers

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

like image 189
Palladium Avatar answered Oct 07 '22 01:10

Palladium