我想将arebic数字转换为英语,我尝试下面的代码

时间:2016-03-01 04:41:16

标签: php

  app.get('/initialize_all_pictures', function(req, res){
  var path = './images/';
  fs.readdir(path, function(err, items){
    if (err){
      console.log("there was an error");
      return;
    }
    console.log(items.length);
    for(var i = 0; i<items.length; i++){
      var photo = new Photo(path + items[i], 0, 0,Math.floor(Math.random()*1000))
      photoArray.push(photo);
    }

  });
  res.json({"Success" : "Done"});

});

我在结果中得到相同的arebic数字。它没有转换为英文数字。

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码执行此操作: -

<?php

function str_split_unicode($str, $length = 1) {
    $tmp = preg_split('~~u', $str, -1, PREG_SPLIT_NO_EMPTY);
    if ($length > 1) {
        $chunks = array_chunk($tmp, $length);
        foreach ($chunks as $i => $chunk) {
            $chunks[$i] = join('', (array) $chunk);
        }
        $tmp = $chunks;
    }
    return $tmp;
}

function convert($string) {
    $data = '';
    $persian = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
    $num = range(0, 9);
    foreach($string as $new_str){
        $data .= str_replace($persian, $num, $new_str);
    }
    return $data;
}
$data = str_split_unicode('۰۲۳'); // convert number into single dimensional array

echo $new_data = convert($data); // pass that array to convert function and then concatenate the whole data to get final English number
?>

输出: - https://eval.in/527884

从这里引用并修改: - convert Persian/Arabic numbers to English numbers