簡單介紹 PHP printf() 使用方法
Example 1
<?php
$num = 5;
$location = 'tree';
$format = 'There are %d monkeys in the %s';
printf($format, $num, $location);
?>
$num = 5;
$location = 'tree';
$format = 'There are %d monkeys in the %s';
printf($format, $num, $location);
?>
%d 為整數 %s 為字串
因此結果會是 There ara 5 monkeys in the tree
Example 2
<?php
$format = 'The %s contains %d monkeys';
printf($format, $num, $location);
?>
$format = 'The %s contains %d monkeys';
printf($format, $num, $location);
?>
此範例 %s 與 %d 必須對調 才可正常執行
Example 3
<?php
$format = 'The %2$s contains %1$d monkeys';
printf($format, $num, $location);
?>
$format = 'The %2$s contains %1$d monkeys';
printf($format, $num, $location);
?>
若在 Example 2 $num 及 $location 不想對調 可以使用此方法
%2$s 即指定順序第 2 的字串 即 $location
%1$d 即指定順序第 1 的整數 即 $num
Example 4
<?php
$format = 'The %2$s contains %1$d monkeys.
That\'s a nice %2$s full of %1$d monkeys.';
printf($format, $num, $location);
?>
$format = 'The %2$s contains %1$d monkeys.
That\'s a nice %2$s full of %1$d monkeys.';
printf($format, $num, $location);
?>
若不希望在 printf 中置入太多參數
我們可以重複引用
%2$s 使用兩次 %1$d 也使用兩次
結果會是 The tree contains 5 monkeys.That's a nice tree full of 5 monkeys.
Example 5
<?php
$format= 'The %2$s contains %1$04d monkeys';
printf($format, $num, $location);
?>
$format= 'The %2$s contains %1$04d monkeys';
printf($format, $num, $location);
?>
若想使整數補滿指定位數 可以指定前置位數
%1$04d 即是指定 $num 為四位數 未滿則補 0
因此結果會是 The tree contains 0005 monkeys
Example 6
<?php
$n = 43951789;
$u = -43951789;
$c = 65; // ASCII 65 is 'A'
// notice the double %%, this prints a literal '%' character
printf("%%b = '%b'\n", $n); // binary representation
printf("%%c = '%c'\n", $c); // print the ascii character, same as chr() function
printf("%%d = '%d'\n", $n); // standard integer representation
printf("%%e = '%e'\n", $n); // scientific notation
printf("%%u = '%u'\n", $n); // unsigned integer representation of a positive integer
printf("%%u = '%u'\n", $u); // unsigned integer representation of a negative integer
printf("%%f = '%f'\n", $n); // floating point representation
printf("%%o = '%o'\n", $n); // octal representation
printf("%%s = '%s'\n", $n); // string representation
printf("%%x = '%x'\n", $n); // hexadecimal representation (lower-case)
printf("%%X = '%X'\n", $n); // hexadecimal representation (upper-case)
printf("%%+d = '%+d'\n", $n); // sign specifier on a positive integer
printf("%%+d = '%+d'\n", $u); // sign specifier on a negative integer
?>
$n = 43951789;
$u = -43951789;
$c = 65; // ASCII 65 is 'A'
// notice the double %%, this prints a literal '%' character
printf("%%b = '%b'\n", $n); // binary representation
printf("%%c = '%c'\n", $c); // print the ascii character, same as chr() function
printf("%%d = '%d'\n", $n); // standard integer representation
printf("%%e = '%e'\n", $n); // scientific notation
printf("%%u = '%u'\n", $n); // unsigned integer representation of a positive integer
printf("%%u = '%u'\n", $u); // unsigned integer representation of a negative integer
printf("%%f = '%f'\n", $n); // floating point representation
printf("%%o = '%o'\n", $n); // octal representation
printf("%%s = '%s'\n", $n); // string representation
printf("%%x = '%x'\n", $n); // hexadecimal representation (lower-case)
printf("%%X = '%X'\n", $n); // hexadecimal representation (upper-case)
printf("%%+d = '%+d'\n", $n); // sign specifier on a positive integer
printf("%%+d = '%+d'\n", $u); // sign specifier on a negative integer
?>
結果如下
%b = '10100111101010011010101101'
%c = 'A'
%d = '43951789'
%e = '4.39518e+7'
%u = '43951789'
%u = '4251015507'
%f = '43951789.000000'
%o = '247523255'
%s = '43951789'
%x = '29ea6ad'
%X = '29EA6AD'
%+d = '+43951789'
%+d = '-43951789'
%c = 'A'
%d = '43951789'
%e = '4.39518e+7'
%u = '43951789'
%u = '4251015507'
%f = '43951789.000000'
%o = '247523255'
%s = '43951789'
%x = '29ea6ad'
%X = '29EA6AD'
%+d = '+43951789'
%+d = '-43951789'
兩個表示 % 符號 表示跳脫字元 即 %% print 出來的結果會是 %
Example 7
<?php
$s = 'monkey';
$t = 'many monkeys';
printf("[%s]\n", $s); // standard string output
printf("[%10s]\n", $s); // right-justification with spaces
printf("[%-10s]\n", $s); // left-justification with spaces
printf("[%010s]\n", $s); // zero-padding works on strings too
printf("[%'#10s]\n", $s); // use the custom padding character '#'
printf("[%10.10s]\n", $t); // left-justification but with a cutoff of 10 characters
?>
$s = 'monkey';
$t = 'many monkeys';
printf("[%s]\n", $s); // standard string output
printf("[%10s]\n", $s); // right-justification with spaces
printf("[%-10s]\n", $s); // left-justification with spaces
printf("[%010s]\n", $s); // zero-padding works on strings too
printf("[%'#10s]\n", $s); // use the custom padding character '#'
printf("[%10.10s]\n", $t); // left-justification but with a cutoff of 10 characters
?>
結果如下
[monkey]
[ monkey]
[monkey ]
[0000monkey]
[####monkey]
[many monke]
[ monkey]
[monkey ]
[0000monkey]
[####monkey]
[many monke]
以上為 printf 簡單使用方式
原文網址
全站熱搜