Ehsan Ehsan - 1 month ago 19
PHP Question

str_replace don't work for some chars

I wrote below code but it works wrong:

$t = "۰۱۲۳۴۵۶۷۸۹٤٥٦";
$correct = array("0","1","2","3","4","5","6","7","8","9","4","5","6");
$wrong = array("٠","١","٢","٣","٤","٥","٦","٧","٨","٩","۴","۵","۶");
echo $t = str_replace($wrong,$correct, $t);


and the result isn't right

۰۱۲۳456۷۸۹۰۱۲۳456۷۸۹456


I search in stack and same questions and it's answers don't help me.

Answer Source

I don`t know any about the problem, but you can try this function to implement the idea:

//converts the numbers into the persian's number
function persianNum($srting)
{
    $num0="۰";
    $num1="۱";
    $num2="۲";
    $num3="۳";
    $num4="۴";
    $num5="۵";
    $num6="۶";
    $num7="۷";
    $num8="۸";
    $num9="۹";

    $stringtemp="";
    $len=strlen($srting);
    for($sub=0;$sub<$len;$sub++)
    {
         if(substr($srting,$sub,1)=="0")$stringtemp.=$num0;
         elseif(substr($srting,$sub,1)=="1")$stringtemp.=$num1;
         elseif(substr($srting,$sub,1)=="2")$stringtemp.=$num2;
         elseif(substr($srting,$sub,1)=="3")$stringtemp.=$num3;
         elseif(substr($srting,$sub,1)=="4")$stringtemp.=$num4;
         elseif(substr($srting,$sub,1)=="5")$stringtemp.=$num5;
         elseif(substr($srting,$sub,1)=="6")$stringtemp.=$num6;
         elseif(substr($srting,$sub,1)=="7")$stringtemp.=$num7;
         elseif(substr($srting,$sub,1)=="8")$stringtemp.=$num8;
         elseif(substr($srting,$sub,1)=="9")$stringtemp.=$num9;
         else $stringtemp.=substr($srting,$sub,1);
    }
return   $stringtemp;

}
//end convert function