Title: Convert a string 1234567890 to 1,234,567,890
Solution One: The two strings in reverse view will be 0987654321 converted to 098,765,432,1, then you can 0987654321 The array (098,765,432,1) is divided into three length strings, then the array and comma are combined into a string and then reversed to get the final result .
$str= ' 1234567890 ';functionChange$str){ $str 1=Strrev($str);//reverses the string first $arr=Str_split($str 1, 3);//array that is split into 3 for each cell string length $str 2=implode($arr,‘,‘);//combine strings and, in combination, into strings Echo Strrev($str 2);//and the final result is reversed .}change ($str);
Solution Two: You can use the function chunk_split ($str, length, $s) after every three substrings of 0987654321 with a comma, and then reverse back to remove the left comma.
$str= ' 1234567890 ';functionGet$str){ $str 1=Strrev($str);//reverses the string first $str 2=Chunk_split($str 1, 3, ', ');//after each substring with a length of 3 is added, $STR 3=Strrev($str 2);//Reverse Back $STR 4=LTrim($STR 3,‘,‘);//Remove the left, Echo $STR 4;} Get ($str);
Solution Three: Can be 1234567890 as a number, then you can directly use the numeric Format function Number_format () function directly to obtain the desired result
$str= ' 1234567890 '; $str 1=number_format($str); Echo $str 1, ' <br> ';
PHP string Exercises