<?php
//未考虑浮点型
/*
* method 1
* echo number_format($str,2,'.',',');
*/
/* method2
* 先反转字符串strrev 再str_split($str,3);
$str = strrev($str);
$arr = str_split($str,3);//987
$res = '';
$count = count($arr);
while($count--){
$res .= strrev($arr[$count]).',';
}
$res = rtrim($res,',');
*/
/* method 3
* 截取出每3个字符
* $count = strlen($str);
$i = 0;
$md = $count % 3;
switch ($md){
case 0:
break;
case 1:
$res = $str{0}.',';
$count -=1;
$i = 1;
break;
case 2:
$res = substr($str,0,2).',';
$count -= 2;
$i = 2;
break;
}
for(;$i<$count-3;$i+=3){
$res .= substr($str,$i,3).',';
}
$res .= substr($str,$i,3);
*/
/* method 4
* 正则,求不确定长度的做法
$md = strlen($str) % 3;
$res = substr($str, 0,$md).($md == 0?'':',');
$res .= preg_replace('(d{3})', ' ,', substr($str, $md));
$res = rtrim($res,',');
*/
?>