3

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

$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

4 回答 4

2

使用此代码:-

$t = "۰۱۲۳٤٥٦";
$text = preg_replace(
            array("٠","١","٢","٣","٤","٥","٦",),
            array("0","1","2","3","4","5","6",),
            $t );
于 2012-11-20T07:00:57.363 回答
1

试试 'mb_str_replace()' 函数。

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

于 2012-11-20T06:43:27.107 回答
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
于 2012-11-20T07:58:49.267 回答
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;
}
于 2017-08-16T13:21:25.313 回答