<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >sprintf</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="String 字符串处理函数" HREF="ref.strings.html"><LINK REL="PREVIOUS" TITLE="soundex" HREF="function.soundex.html"><LINK REL="NEXT" TITLE="sscanf" HREF="function.sscanf.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.soundex.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.sscanf.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.sprintf" ></A >sprintf</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN226835" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >sprintf -- Return a formatted string</DIV ><DIV CLASS="refsect1" ><A NAME="AEN226838" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >sprintf</B > ( string format [, mixed args [, mixed ...]] )<BR ></BR ><P > Returns a string produced according to the formatting string <CODE CLASS="parameter" >format</CODE >. </P ><P > The format string is composed of zero or more directives: ordinary characters (excluding <TT CLASS="literal" >%</TT >) that are copied directly to the result, and <SPAN CLASS="emphasis" ><I CLASS="emphasis" >conversion specifications</I ></SPAN >, each of which results in fetching its own parameter. This applies to both <B CLASS="function" >sprintf()</B > and <A HREF="function.printf.html" ><B CLASS="function" >printf()</B ></A >. </P ><P > Each conversion specification consists of a percent sign (<TT CLASS="literal" >%</TT >), followed by one or more of these elements, in order: <P ></P ><OL TYPE="1" ><LI ><P > An optional <SPAN CLASS="emphasis" ><I CLASS="emphasis" >sign specifier</I ></SPAN > that forces a sign (- or +) to be used on a number. By default, only the - sign is used on a number if it's negative. This specifier forces positive numbers to have the + sign attached as well, and was added in PHP 4.3.0. </P ></LI ><LI ><P > An optional <SPAN CLASS="emphasis" ><I CLASS="emphasis" >padding specifier</I ></SPAN > that says what character will be used for padding the results to the right string size. This may be a space character or a <TT CLASS="literal" >0</TT > (zero character). The default is to pad with spaces. An alternate padding character can be specified by prefixing it with a single quote (<TT CLASS="literal" >'</TT >). See the examples below. </P ></LI ><LI ><P > An optional <SPAN CLASS="emphasis" ><I CLASS="emphasis" >alignment specifier</I ></SPAN > that says if the result should be left-justified or right-justified. The default is right-justified; a <TT CLASS="literal" >-</TT > character here will make it left-justified. </P ></LI ><LI ><P > An optional number, a <SPAN CLASS="emphasis" ><I CLASS="emphasis" >width specifier</I ></SPAN > that says how many characters (minimum) this conversion should result in. </P ></LI ><LI ><P > An optional <SPAN CLASS="emphasis" ><I CLASS="emphasis" >precision specifier</I ></SPAN > that says how many decimal digits should be displayed for floating-point numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit to the string. </P ></LI ><LI ><P > A <SPAN CLASS="emphasis" ><I CLASS="emphasis" >type specifier</I ></SPAN > that says what type the argument data should be treated as. Possible types: <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD > <TT CLASS="literal" >%</TT > - a literal percent character. No argument is required. </TD ></TR ><TR ><TD > <TT CLASS="literal" >b</TT > - the argument is treated as an integer, and presented as a binary number. </TD ></TR ><TR ><TD > <TT CLASS="literal" >c</TT > - the argument is treated as an integer, and presented as the character with that ASCII value. </TD ></TR ><TR ><TD > <TT CLASS="literal" >d</TT > - the argument is treated as an integer, and presented as a (signed) decimal number. </TD ></TR ><TR ><TD > <TT CLASS="literal" >e</TT > - the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of significant digits (one less). </TD ></TR ><TR ><TD > <TT CLASS="literal" >u</TT > - the argument is treated as an integer, and presented as an unsigned decimal number. </TD ></TR ><TR ><TD > <TT CLASS="literal" >f</TT > - the argument is treated as a float, and presented as a floating-point number (locale aware). </TD ></TR ><TR ><TD > <TT CLASS="literal" >F</TT > - the argument is treated as a float, and presented as a floating-point number (non-locale aware). Available since PHP 4.3.10 and PHP 5.0.3. </TD ></TR ><TR ><TD > <TT CLASS="literal" >o</TT > - the argument is treated as an integer, and presented as an octal number. </TD ></TR ><TR ><TD > <TT CLASS="literal" >s</TT > - the argument is treated as and presented as a string. </TD ></TR ><TR ><TD > <TT CLASS="literal" >x</TT > - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). </TD ></TR ><TR ><TD > <TT CLASS="literal" >X</TT > - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters). </TD ></TR ></TBODY ></TABLE ><P ></P > </P ></LI ></OL > </P ><P > As of PHP 4.0.6 the format string supports argument numbering/swapping. Here is an example: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN226909" ></A ><P ><B >例 1. Argument swapping</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$format </font><font color="#007700">= </font><font color="#DD0000">'There are %d monkeys in the %s'</font><font color="#007700">;<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#0000BB">$format</font><font color="#007700">, </font><font color="#0000BB">$num</font><font color="#007700">, </font><font color="#0000BB">$location</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > This might output, "There are 5 monkeys in the tree". But imagine we are creating a format string in a separate file, commonly because we would like to internationalize it and we rewrite it as: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN226912" ></A ><P ><B >例 2. Argument swapping</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$format </font><font color="#007700">= </font><font color="#DD0000">'The %s contains %d monkeys'</font><font color="#007700">;<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#0000BB">$format</font><font color="#007700">, </font><font color="#0000BB">$num</font><font color="#007700">, </font><font color="#0000BB">$location</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > We now have a problem. The order of the placeholders in the format string does not match the order of the arguments in the code. We would like to leave the code as is and simply indicate in the format string which arguments the placeholders refer to. We would write the format string like this instead: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN226915" ></A ><P ><B >例 3. Argument swapping</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$format </font><font color="#007700">= </font><font color="#DD0000">'The %2$s contains %1$d monkeys'</font><font color="#007700">;<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#0000BB">$format</font><font color="#007700">, </font><font color="#0000BB">$num</font><font color="#007700">, </font><font color="#0000BB">$location</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > An added benefit here is that you can repeat the placeholders without adding more arguments in the code. For example: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN226918" ></A ><P ><B >例 4. Argument swapping</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$format </font><font color="#007700">= </font><font color="#DD0000">'The %2$s contains %1$d monkeys.<br /> That\'s a nice %2$s full of %1$d monkeys.'</font><font color="#007700">;<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#0000BB">$format</font><font color="#007700">, </font><font color="#0000BB">$num</font><font color="#007700">, </font><font color="#0000BB">$location</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.printf.html" ><B CLASS="function" >printf()</B ></A >, <A HREF="function.sscanf.html" ><B CLASS="function" >sscanf()</B ></A >, <A HREF="function.fscanf.html" ><B CLASS="function" >fscanf()</B ></A >, <A HREF="function.vsprintf.html" ><B CLASS="function" >vsprintf()</B ></A >, and <A HREF="function.number-format.html" ><B CLASS="function" >number_format()</B ></A >. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN226927" ></A ><H2 >Examples</H2 ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN226929" ></A ><P ><B >例 5. <A HREF="function.printf.html" ><B CLASS="function" >printf()</B ></A >: various examples</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$n </font><font color="#007700">= </font><font color="#0000BB">43951789</font><font color="#007700">;<br /></font><font color="#0000BB">$u </font><font color="#007700">= -</font><font color="#0000BB">43951789</font><font color="#007700">;<br /></font><font color="#0000BB">$c </font><font color="#007700">= </font><font color="#0000BB">65</font><font color="#007700">; </font><font color="#FF8000">// ASCII 65 is 'A'<br /><br />// notice the double %%, this prints a literal '%' character<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%b = '%b'\n"</font><font color="#007700">, </font><font color="#0000BB">$n</font><font color="#007700">); </font><font color="#FF8000">// binary representation<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%c = '%c'\n"</font><font color="#007700">, </font><font color="#0000BB">$c</font><font color="#007700">); </font><font color="#FF8000">// print the ascii character, same as chr() function<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%d = '%d'\n"</font><font color="#007700">, </font><font color="#0000BB">$n</font><font color="#007700">); </font><font color="#FF8000">// standard integer representation<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%e = '%e'\n"</font><font color="#007700">, </font><font color="#0000BB">$n</font><font color="#007700">); </font><font color="#FF8000">// scientific notation<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%u = '%u'\n"</font><font color="#007700">, </font><font color="#0000BB">$n</font><font color="#007700">); </font><font color="#FF8000">// unsigned integer representation of a positive integer<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%u = '%u'\n"</font><font color="#007700">, </font><font color="#0000BB">$u</font><font color="#007700">); </font><font color="#FF8000">// unsigned integer representation of a negative integer<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%f = '%f'\n"</font><font color="#007700">, </font><font color="#0000BB">$n</font><font color="#007700">); </font><font color="#FF8000">// floating point representation<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%o = '%o'\n"</font><font color="#007700">, </font><font color="#0000BB">$n</font><font color="#007700">); </font><font color="#FF8000">// octal representation<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%s = '%s'\n"</font><font color="#007700">, </font><font color="#0000BB">$n</font><font color="#007700">); </font><font color="#FF8000">// string representation<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%x = '%x'\n"</font><font color="#007700">, </font><font color="#0000BB">$n</font><font color="#007700">); </font><font color="#FF8000">// hexadecimal representation (lower-case)<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%X = '%X'\n"</font><font color="#007700">, </font><font color="#0000BB">$n</font><font color="#007700">); </font><font color="#FF8000">// hexadecimal representation (upper-case)<br /><br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%+d = '%+d'\n"</font><font color="#007700">, </font><font color="#0000BB">$n</font><font color="#007700">); </font><font color="#FF8000">// sign specifier on a positive integer<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%%+d = '%+d'\n"</font><font color="#007700">, </font><font color="#0000BB">$u</font><font color="#007700">); </font><font color="#FF8000">// sign specifier on a negative integer<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > The printout of this program would be: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >%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'</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN226935" ></A ><P ><B >例 6. <A HREF="function.printf.html" ><B CLASS="function" >printf()</B ></A >: string specifiers</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$s </font><font color="#007700">= </font><font color="#DD0000">'monkey'</font><font color="#007700">;<br /></font><font color="#0000BB">$t </font><font color="#007700">= </font><font color="#DD0000">'many monkeys'</font><font color="#007700">;<br /><br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"[%s]\n"</font><font color="#007700">, </font><font color="#0000BB">$s</font><font color="#007700">); </font><font color="#FF8000">// standard string output<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"[%10s]\n"</font><font color="#007700">, </font><font color="#0000BB">$s</font><font color="#007700">); </font><font color="#FF8000">// right-justification with spaces<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"[%-10s]\n"</font><font color="#007700">, </font><font color="#0000BB">$s</font><font color="#007700">); </font><font color="#FF8000">// left-justification with spaces<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"[%010s]\n"</font><font color="#007700">, </font><font color="#0000BB">$s</font><font color="#007700">); </font><font color="#FF8000">// zero-padding works on strings too<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"[%'#10s]\n"</font><font color="#007700">, </font><font color="#0000BB">$s</font><font color="#007700">); </font><font color="#FF8000">// use the custom padding character '#'<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"[%10.10s]\n"</font><font color="#007700">, </font><font color="#0000BB">$t</font><font color="#007700">); </font><font color="#FF8000">// left-justification but with a cutoff of 10 characters<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > The printout of this program would be: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >[monkey] [ monkey] [monkey ] [0000monkey] [####monkey] [many monke]</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN226941" ></A ><P ><B >例 7. <B CLASS="function" >sprintf()</B >: zero-padded integers</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$isodate </font><font color="#007700">= </font><font color="#0000BB">sprintf</font><font color="#007700">(</font><font color="#DD0000">"%04d-%02d-%02d"</font><font color="#007700">, </font><font color="#0000BB">$year</font><font color="#007700">, </font><font color="#0000BB">$month</font><font color="#007700">, </font><font color="#0000BB">$day</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN226945" ></A ><P ><B >例 8. <B CLASS="function" >sprintf()</B >: formatting currency</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$money1 </font><font color="#007700">= </font><font color="#0000BB">68.75</font><font color="#007700">;<br /></font><font color="#0000BB">$money2 </font><font color="#007700">= </font><font color="#0000BB">54.35</font><font color="#007700">;<br /></font><font color="#0000BB">$money </font><font color="#007700">= </font><font color="#0000BB">$money1 </font><font color="#007700">+ </font><font color="#0000BB">$money2</font><font color="#007700">;<br /></font><font color="#FF8000">// echo $money will output "123.1";<br /></font><font color="#0000BB">$formatted </font><font color="#007700">= </font><font color="#0000BB">sprintf</font><font color="#007700">(</font><font color="#DD0000">"%01.2f"</font><font color="#007700">, </font><font color="#0000BB">$money</font><font color="#007700">);<br /></font><font color="#FF8000">// echo $formatted will output "123.10"<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN226949" ></A ><P ><B >例 9. <B CLASS="function" >sprintf()</B >: scientific notation</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$number </font><font color="#007700">= </font><font color="#0000BB">362525200</font><font color="#007700">;<br /><br />echo </font><font color="#0000BB">sprintf</font><font color="#007700">(</font><font color="#DD0000">"%.3e"</font><font color="#007700">, </font><font color="#0000BB">$number</font><font color="#007700">); </font><font color="#FF8000">// outputs 3.625e+8<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.soundex.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.sscanf.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >soundex</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.strings.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >sscanf</TD ></TR ></TABLE ></DIV ></BODY ></HTML >