str_replace不适用于某些字符

时间:2012-11-20 06:38:18

标签: php str-replace arabic numeric

我写了下面的代码,但它的工作错误:

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

,结果不对

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

我在堆栈中搜索相同的问题而且它的答案对我没有帮助。

4 个答案:

答案 0 :(得分:2)

  

使用此代码: -

$t = "۰۱۲۳٤٥٦";
$text = preg_replace(
            array("٠","١","٢","٣","٤","٥","٦",),
            array("0","1","2","3","4","5","6",),
            $t );

答案 1 :(得分:1)

尝试' mb_str_replace()'功能

http://php.net/manual/en/ref.mbstring.php

答案 2 :(得分:1)

  

我对这个问题一无所知,但你可以试试这个功能来实现这个想法:

//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

答案 3 :(得分:0)

在PHP中没有mb_str_repalce函数。此代码将字符串中的字符从东阿拉伯数字转换为印度 - 阿拉伯数字:

<?php
function arabic_convert($str = ''){
    $str_list = preg_split('//u', $str, null, PREG_SPLIT_NO_EMPTY);

    if( !empty($str_list) ){
        $transform_symbols = array(
            '٠' => '0',
            '١' => '1',
            '٢' => '2',
            '٣' => '3',
            '٤' => '4',
            '٥' => '5',
            '٦' => '6',
            '٧' => '7',
            '٨' => '8',
            '٩' => '9',
        );

        $str_transform = array();

        foreach ($str_list as $str_item){
            if( isset($transform_symbols[$str_item]) ){
                $str_transform[] = $transform_symbols[$str_item];
            }else{
                $str_transform[] = $str_item;
            }
        }

        $str = implode('', $str_transform);
    }

    return $str;
}