源代码:
<HTML> <HEAD></HEAD> <FONT FACE="'Courier New',Verdana, Arial, Helvetica" SIZE=2> <!DOCTYPE html> <html> <body> <FONT COLOR=#ff0000><?php $str1 = 'Hello'; $str2 = 'Hello world!'; echo sprintf("[%s]",$str1)." "; // String echo sprintf("[%8s]",$str1)." "; // Right-justifies the string with spaces echo sprintf("[%-8s]",$str1)." "; // Left-justifies the string with spaces echo sprintf("[%08s]",$str1)." "; // Zero-padding echo sprintf("[%'*8s]",$str1)." "; // Adds "*" echo sprintf("[%8.8s]",$str2)." "; // Left-justifies the string with spaces (cuts off characters after the specified value) ?></FONT> </body> </html> </HTML>
运行结果:
<HTML> <HEAD></HEAD> <FONT FACE="'Courier New',Verdana, Arial, Helvetica" SIZE=2> <!DOCTYPE html> <html> <body> <FONT COLOR=#ff0000>[Hello] [ Hello] [Hello ] [000Hello] [***Hello] [Hello wo] </FONT> </body> </html> </HTML>