Convert a string (1234567890) into (1,234,567,890) with each group of three separated by commas

WBOY
Release: 2016-07-28 08:27:34
Original
4166 people have browsed it

/**
* Method 1 PHP built-in function
*/
// $str = '1234567890';
// $newstr = number_format($str);
// echo $str;
// echo '
';
// echo $newstr;
/**
* Method 2: PHP’s built-in function implementation
*/
$str = '1234567890'; //The number to be formatted
$count = 3; //Split every few numbers
echo $str;
echo '
';
echo test($str,$count);
function test($str='',$count=3){
if(empty($str ) || $count <= 0){
                                                                            return false; Split the string into an array
        $new_str = join(',',$arr); // Join the string
                                                    out out through ’s
    ’     out out out out out out through out through out through through out through off‐‐to ‐‐‐‐‐‐ and } to split the string into arrays
                     The built-in function


number_format()

 将一个字符串(1234567890)转换成(1,234,567,890)每三个一组用逗号隔开$number

=

1234. 56
;// english notation (default )
$english_format_number
=
number_format($number);// 1,235// French notation
$nombre_ format_francais
=
number_format( $number, 2, ',', ' ');// 1 234,56
$number
=
1234.5678; // english notation without thousands and separator
$english_format_number
=
number_format($number, 2, '.', '' );// 1234.57
?>
strrev()

returns

the reversed string.

string

echo

strrev
("Hello world!"); // Output "!dlrow olleH"?>
implode() (alias join())

concatenates the values ​​​​of one-dimensional arrays into a string

$array = array( 'lastname'
, 'email', 'phone');$comma_separated =
implode(", ", $array );echo $comma_separated;
//lastname,email,phone// Empty string when using an empty array:var_dump
(
implode ('hello ', array())); // string(0) ""?>
str_split()


The above introduces how to convert a string (1234567890) into (1,234,567,890). Each group of three is separated by commas, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template