-
- //Floating point type is not considered
- /*
- * method 1
- * echo number_format($str,2,'.',',');
- */
- /* method2
- * First reverse the string strrev and then 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
- * Cut out every 3 characters
- * $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
- * Regular way to find uncertain length
- $md = strlen($str) % 3;
- $res = substr($str, 0,$md).($md == 0?'':',');
- $res .= preg_replace('(d{3})', '\0,', substr($str, $md));
- $res = rtrim($res,',');
- */
- ?>
Copy code
|