doujia7162 2012-04-03 03:06
浏览 95
已采纳

too long

I'm trying to write function to do the number_format() job for the non-ascii numbers , specifically perso-arabic numbers.

First i have to exchange the numbers which leaves me with a string of non-ascii characters:

$n = 133;
$n = exchange($n);
echo $n ;
//result : ١٣٣

The problem is when I add the commas to the number or rather string, my final result comes with some � characters.

Here is the function that I use to add the commas:

    static public function addcomma($number)
       {

    $i = strlen($number)-1;
    $c = 0 ;

    for($i ; $i >= 0 ; $i--){
    $c++;

    if($c == 1 ) 
    $y =mb_substr($number, $i, 1);
    else 
    $y .= mb_substr($number, $i, 1);



    if($c%3 == 0 && $i != 0 )
    $y .=',';
    }
    $y = strrev($y);
    return $y;

    }

And this is the result for $n = ١٣٣:

٣,٣�,�١

  • 写回答

2条回答 默认 最新

  • doushi5117 2012-04-03 03:14
    关注

    Some of your characters (likely all) are stored on more than one byte, unline regular ASCII strings. So you have to use multibyte string functions to manipulate the strings. You can't use strlen, substr and strrev (or any other regular string function), and you can't just treat the string as an array. So, you have to change some sections of your code, like this:

    $i = mb_strlen($number)-1;
    // (...)
    $y = mb_substr($number, $i, 1);
    

    There is no multibyte equivalent for strrev, so you can try this (suggested on a comment at the strrev manual page):

    // strrev won't work
    // $y = strrev($y); 
    $y = join("", array_reverse(preg_split("//u", $y)));
    

    The above will split the string into an array, respecting the multibyte boundaries (note the u at the end of the regex), reverse that array, and then join it back to a string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)