root root - 3 months ago 31
PHP Question

convert Persian/Arabic numbers to English numbers

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="۹";


MyFunction / Converting numbers to English:

function Convertnumber2english($srting) {
$num0="0";
$num1="1";
$num2="2";
$num3="3";
$num4="4";
$num5="5";
$num6="6";
$num7="7";
$num8="8";
$num9="9";

$stringtemp="";
$len=strlen($srting);
for($sub=0;$sub<$len;$sub++){
if(substr($srting,$sub,1)=="۰")$stringtemp.=$num0;
elseif(substr($srting,$sub,1)=="۱")$stringtemp.=$num1;
elseif(substr($srting,$sub,1)=="۲")$stringtemp.=$num2;
elseif(substr($srting,$sub,1)=="۳")$stringtemp.=$num3;
elseif(substr($srting,$sub,1)=="۴")$stringtemp.=$num4;
elseif(substr($srting,$sub,1)=="۵")$stringtemp.=$num5;
elseif(substr($srting,$sub,1)=="۶")$stringtemp.=$num6;
elseif(substr($srting,$sub,1)=="۷")$stringtemp.=$num7;
elseif(substr($srting,$sub,1)=="۸")$stringtemp.=$num8;
elseif(substr($srting,$sub,1)=="۹")$stringtemp.=$num9;
else $stringtemp.=substr($srting,$sub,1);
}
return $stringtemp;
}

Answer

Here's a short function:

function convert($string) {
    $persian = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
    $num = range(0, 9);
    return str_replace($persian, $num, $string);
}

You can use the unicode instead of the characters in $persian (I think).