mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 05:26:57 +08:00
463 lines
31 KiB
HTML
463 lines
31 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<title>Return a formatted string</title>
|
|
</head>
|
|
<body class="docs"><div id="layout">
|
|
<div id="layout-content"><div id="function.sprintf" class="refentry">
|
|
<div class="refnamediv">
|
|
<h1 class="refname">sprintf</h1>
|
|
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">sprintf</span> — <span class="dc-title">Return a formatted string</span></p>
|
|
|
|
</div>
|
|
|
|
<div class="refsect1 description" id="refsect1-function.sprintf-description">
|
|
<h3 class="title">说明</h3>
|
|
<div class="methodsynopsis dc-description">
|
|
<span class="methodname"><strong>sprintf</strong></span>
|
|
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>
|
|
[, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
|
|
] ) : <span class="type">string</span></div>
|
|
|
|
<p class="para rdfs-comment">
|
|
Returns a string produced according to the formatting string
|
|
<code class="parameter">format</code>.
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 parameters" id="refsect1-function.sprintf-parameters">
|
|
<h3 class="title">参数</h3>
|
|
<p class="para">
|
|
<dl>
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">format</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The format string is composed of zero or more directives:
|
|
ordinary characters (excluding <em>%</em>) that are
|
|
copied directly to the result and <em class="emphasis">conversion
|
|
specifications</em>, each of which results in fetching its
|
|
own parameter. This applies to both <span class="function"><strong>sprintf()</strong></span>
|
|
and <span class="function"><a href="printf.html" class="function">printf()</a></span>.
|
|
</p>
|
|
<p class="para">
|
|
Each conversion specification consists of a percent sign
|
|
(<em>%</em>), followed by one or more of these
|
|
elements, in order:
|
|
<ol type="1">
|
|
<li class="listitem">
|
|
<span class="simpara">
|
|
An optional <em class="emphasis">sign specifier</em> 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.
|
|
</span>
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="simpara">
|
|
An optional <em class="emphasis">padding specifier</em> that says
|
|
what character will be used for padding the results to the
|
|
right string size. This may be a space character or a
|
|
<em>0</em> (zero character). The default is to pad
|
|
with spaces. An alternate padding character can be specified
|
|
by prefixing it with a single quote (<em>'</em>).
|
|
See the examples below.
|
|
</span>
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="simpara">
|
|
An optional <em class="emphasis">alignment specifier</em> that says
|
|
if the result should be left-justified or right-justified.
|
|
The default is right-justified; a <em>-</em>
|
|
character here will make it left-justified.
|
|
</span>
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="simpara">
|
|
An optional number, a <em class="emphasis">width specifier</em>
|
|
that says how many characters (minimum) this conversion should
|
|
result in.
|
|
</span>
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="simpara">
|
|
An optional <em class="emphasis">precision specifier</em> in the form
|
|
of a period (<em>.</em>) followed by an optional decimal digit string
|
|
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. Additionally, the character to use when padding a
|
|
number may optionally be specified between the period and the
|
|
digit.
|
|
</span>
|
|
</li>
|
|
<li class="listitem">
|
|
<p class="para">
|
|
A <em class="emphasis">type specifier</em> that says what type the
|
|
argument data should be treated as. Possible types:
|
|
<ul class="simplelist">
|
|
<li class="member">
|
|
<em>%</em> - a literal percent character. No
|
|
argument is required.
|
|
</li>
|
|
<li class="member">
|
|
<em>b</em> - the argument is treated as an
|
|
integer and presented as a binary number.
|
|
</li>
|
|
<li class="member">
|
|
<em>c</em> - the argument is treated as an
|
|
integer and presented as the character with that ASCII
|
|
value.
|
|
</li>
|
|
<li class="member">
|
|
<em>d</em> - the argument is treated as an
|
|
integer and presented as a (signed) decimal number.
|
|
</li>
|
|
<li class="member">
|
|
<em>e</em> - 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).
|
|
</li>
|
|
<li class="member">
|
|
<em>E</em> - like <em>%e</em> but uses
|
|
uppercase letter (e.g. 1.2E+2).
|
|
</li>
|
|
<li class="member">
|
|
<em>f</em> - the argument is treated as a
|
|
float and presented as a floating-point number (locale aware).
|
|
</li>
|
|
<li class="member">
|
|
<em>F</em> - the argument is treated as a
|
|
float and presented as a floating-point number (non-locale aware).
|
|
Available since PHP 5.0.3.
|
|
</li>
|
|
<li class="member">
|
|
<em>g</em> - shorter of <em>%e</em> and
|
|
<em>%f</em>.
|
|
</li>
|
|
<li class="member">
|
|
<em>G</em> - shorter of <em>%E</em> and
|
|
<em>%F</em>.
|
|
</li>
|
|
<li class="member">
|
|
<em>o</em> - the argument is treated as an
|
|
integer and presented as an octal number.
|
|
</li>
|
|
<li class="member">
|
|
<em>s</em> - the argument is treated as and
|
|
presented as a string.
|
|
</li>
|
|
<li class="member">
|
|
<em>u</em> - the argument is treated as an
|
|
integer and presented as an unsigned decimal number.
|
|
</li>
|
|
<li class="member">
|
|
<em>x</em> - the argument is treated as an integer
|
|
and presented as a hexadecimal number (with lowercase
|
|
letters).
|
|
</li>
|
|
<li class="member">
|
|
<em>X</em> - the argument is treated as an integer
|
|
and presented as a hexadecimal number (with uppercase
|
|
letters).
|
|
</li>
|
|
</ul>
|
|
</p>
|
|
</li>
|
|
</ol>
|
|
</p>
|
|
<p class="para">
|
|
Variables will be co-erced to a suitable type for the specifier:
|
|
<table id="sprintf.coercion" class="doctable table">
|
|
<caption><strong>Type Handling</strong></caption>
|
|
|
|
<thead>
|
|
<tr>
|
|
<th>Type</th>
|
|
<th>Specifiers</th>
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody class="tbody">
|
|
<tr>
|
|
<td><em>string</em></td>
|
|
<td><em>s</em></td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td><em>integer</em></td>
|
|
<td>
|
|
<em>d</em>,
|
|
<em>u</em>,
|
|
<em>c</em>,
|
|
<em>o</em>,
|
|
<em>x</em>,
|
|
<em>X</em>,
|
|
<em>b</em>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td><em>double</em></td>
|
|
<td>
|
|
<em>g</em>,
|
|
<em>G</em>,
|
|
<em>e</em>,
|
|
<em>E</em>,
|
|
<em>f</em>,
|
|
<em>F</em>
|
|
</td>
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</p>
|
|
<div class="warning"><strong class="warning">Warning</strong>
|
|
<p class="para">
|
|
Attempting to use a combination of the string and width specifiers with character sets that require more than one byte per character may result in unexpected results
|
|
</p>
|
|
</div>
|
|
<p class="para">
|
|
The format string supports argument numbering/swapping. Here is an
|
|
example:
|
|
<div class="example" id="example-5940">
|
|
<p><strong>Example #1 Argument swapping</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$num </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$location </span><span style="color: #007700">= </span><span style="color: #DD0000">'tree'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'There are %d monkeys in the %s'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$num</span><span style="color: #007700">, </span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
</div>
|
|
This will 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:
|
|
<div class="example" id="example-5941">
|
|
<p><strong>Example #2 Argument swapping</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'The %s contains %d monkeys'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$num</span><span style="color: #007700">, </span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
</div>
|
|
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:
|
|
<div class="example" id="example-5942">
|
|
<p><strong>Example #3 Argument swapping</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'The %2$s contains %1$d monkeys'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$num</span><span style="color: #007700">, </span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
</div>
|
|
An added benefit here is that you can repeat the placeholders without
|
|
adding more arguments in the code. For example:
|
|
<div class="example" id="example-5943">
|
|
<p><strong>Example #4 Argument swapping</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'The %2$s contains %1$d monkeys.<br /> That\'s a nice %2$s full of %1$d monkeys.'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$num</span><span style="color: #007700">, </span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
</div>
|
|
When using argument swapping, the <em>n$</em>
|
|
<em class="emphasis">position specifier</em> must come immediately
|
|
after the percent sign (<em>%</em>), before any other
|
|
specifiers, as shown in the example below.
|
|
<div class="example" id="example-5944">
|
|
<p><strong>Example #5 Specifying padding character</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%'.9d\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">123</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%'.09d\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">123</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
<div class="example-contents"><p>以上例程会输出:</p></div>
|
|
<div class="example-contents screen">
|
|
<div class="cdata"><pre>
|
|
......123
|
|
000000123
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="example" id="example-5945">
|
|
<p><strong>Example #6 Position specifier with other specifiers</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'The %2$s contains %1$04d monkeys'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$num</span><span style="color: #007700">, </span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
<div class="example-contents"><p>以上例程会输出:</p></div>
|
|
<div class="example-contents screen">
|
|
<div class="cdata"><pre>
|
|
The tree contains 0005 monkeys
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</p>
|
|
<blockquote class="note"><p><strong class="note">Note</strong>:
|
|
<p class="para">
|
|
Attempting to use a position specifier greater than
|
|
<strong><code>PHP_INT_MAX</code></strong> will result in
|
|
<span class="function"><strong>sprintf()</strong></span> generating warnings.
|
|
</p>
|
|
</p></blockquote>
|
|
<div class="warning"><strong class="warning">Warning</strong>
|
|
<p class="para">
|
|
The <em>c</em> type specifier ignores padding and width
|
|
</p>
|
|
</div>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">...</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 returnvalues" id="refsect1-function.sprintf-returnvalues">
|
|
<h3 class="title">返回值</h3>
|
|
<p class="para">
|
|
Returns a string produced according to the formatting string
|
|
<code class="parameter">format</code>, 或者在失败时返回 <strong><code>FALSE</code></strong>.
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 examples" id="refsect1-function.sprintf-examples">
|
|
<h3 class="title">范例</h3>
|
|
<div class="example" id="example-5946">
|
|
<p><strong>Example #7 <span class="function"><a href="printf.html" class="function">printf()</a></span>: various examples</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$n </span><span style="color: #007700">= </span><span style="color: #0000BB">43951789</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$u </span><span style="color: #007700">= -</span><span style="color: #0000BB">43951789</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">65</span><span style="color: #007700">; </span><span style="color: #FF8000">// ASCII 65 is 'A'<br /><br />// notice the double %%, this prints a literal '%' character<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%b = '%b'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$n</span><span style="color: #007700">); </span><span style="color: #FF8000">// binary representation<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%c = '%c'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$c</span><span style="color: #007700">); </span><span style="color: #FF8000">// print the ascii character, same as chr() function<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%d = '%d'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$n</span><span style="color: #007700">); </span><span style="color: #FF8000">// standard integer representation<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%e = '%e'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$n</span><span style="color: #007700">); </span><span style="color: #FF8000">// scientific notation<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%u = '%u'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$n</span><span style="color: #007700">); </span><span style="color: #FF8000">// unsigned integer representation of a positive integer<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%u = '%u'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$u</span><span style="color: #007700">); </span><span style="color: #FF8000">// unsigned integer representation of a negative integer<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%f = '%f'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$n</span><span style="color: #007700">); </span><span style="color: #FF8000">// floating point representation<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%o = '%o'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$n</span><span style="color: #007700">); </span><span style="color: #FF8000">// octal representation<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%s = '%s'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$n</span><span style="color: #007700">); </span><span style="color: #FF8000">// string representation<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%x = '%x'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$n</span><span style="color: #007700">); </span><span style="color: #FF8000">// hexadecimal representation (lower-case)<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%X = '%X'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$n</span><span style="color: #007700">); </span><span style="color: #FF8000">// hexadecimal representation (upper-case)<br /><br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%+d = '%+d'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$n</span><span style="color: #007700">); </span><span style="color: #FF8000">// sign specifier on a positive integer<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%%+d = '%+d'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$u</span><span style="color: #007700">); </span><span style="color: #FF8000">// sign specifier on a negative integer<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
<div class="example-contents"><p>以上例程会输出:</p></div>
|
|
<div class="example-contents screen">
|
|
<div class="cdata"><pre>
|
|
%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></div>
|
|
</div>
|
|
</div>
|
|
<div class="example" id="example-5947">
|
|
<p><strong>Example #8 <span class="function"><a href="printf.html" class="function">printf()</a></span>: string specifiers</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$s </span><span style="color: #007700">= </span><span style="color: #DD0000">'monkey'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$t </span><span style="color: #007700">= </span><span style="color: #DD0000">'many monkeys'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%s]\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$s</span><span style="color: #007700">); </span><span style="color: #FF8000">// standard string output<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%10s]\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$s</span><span style="color: #007700">); </span><span style="color: #FF8000">// right-justification with spaces<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%-10s]\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$s</span><span style="color: #007700">); </span><span style="color: #FF8000">// left-justification with spaces<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%010s]\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$s</span><span style="color: #007700">); </span><span style="color: #FF8000">// zero-padding works on strings too<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%'#10s]\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$s</span><span style="color: #007700">); </span><span style="color: #FF8000">// use the custom padding character '#'<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%10.10s]\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$t</span><span style="color: #007700">); </span><span style="color: #FF8000">// left-justification but with a cutoff of 10 characters<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
<div class="example-contents"><p>以上例程会输出:</p></div>
|
|
<div class="example-contents screen">
|
|
<div class="cdata"><pre>
|
|
[monkey]
|
|
[ monkey]
|
|
[monkey ]
|
|
[0000monkey]
|
|
[####monkey]
|
|
[many monke]
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="example" id="example-5948">
|
|
<p><strong>Example #9 <span class="function"><strong>sprintf()</strong></span>: zero-padded integers</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$isodate </span><span style="color: #007700">= </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%04d-%02d-%02d"</span><span style="color: #007700">, </span><span style="color: #0000BB">$year</span><span style="color: #007700">, </span><span style="color: #0000BB">$month</span><span style="color: #007700">, </span><span style="color: #0000BB">$day</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
</div>
|
|
<div class="example" id="example-5949">
|
|
<p><strong>Example #10 <span class="function"><strong>sprintf()</strong></span>: formatting currency</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$money1 </span><span style="color: #007700">= </span><span style="color: #0000BB">68.75</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$money2 </span><span style="color: #007700">= </span><span style="color: #0000BB">54.35</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$money </span><span style="color: #007700">= </span><span style="color: #0000BB">$money1 </span><span style="color: #007700">+ </span><span style="color: #0000BB">$money2</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// echo $money will output "123.1";<br /></span><span style="color: #0000BB">$formatted </span><span style="color: #007700">= </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%01.2f"</span><span style="color: #007700">, </span><span style="color: #0000BB">$money</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// echo $formatted will output "123.10"<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
</div>
|
|
<div class="example" id="example-5950">
|
|
<p><strong>Example #11 <span class="function"><strong>sprintf()</strong></span>: scientific notation</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$number </span><span style="color: #007700">= </span><span style="color: #0000BB">362525200</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%.3e"</span><span style="color: #007700">, </span><span style="color: #0000BB">$number</span><span style="color: #007700">); </span><span style="color: #FF8000">// outputs 3.625e+8<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 seealso" id="refsect1-function.sprintf-seealso">
|
|
<h3 class="title">参见</h3>
|
|
<p class="para">
|
|
<ul class="simplelist">
|
|
<li class="member"><span class="function"><a href="printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - 输出格式化字符串</span></li>
|
|
<li class="member"><span class="function"><a href="sscanf.html" class="function" rel="rdfs-seeAlso">sscanf()</a> - 根据指定格式解析输入的字符</span></li>
|
|
<li class="member"><span class="function"><a href="fscanf.html" class="function" rel="rdfs-seeAlso">fscanf()</a> - 从文件中格式化输入</span></li>
|
|
<li class="member"><span class="function"><a href="vsprintf.html" class="function" rel="rdfs-seeAlso">vsprintf()</a> - 返回格式化字符串</span></li>
|
|
<li class="member"><span class="function"><a href="number_format.html" class="function" rel="rdfs-seeAlso">number_format()</a> - 以千位分隔符方式格式化一个数字</span></li>
|
|
<li class="member"><span class="function"><a href="date.html" class="function" rel="rdfs-seeAlso">date()</a> - 格式化一个本地时间/日期</span></li>
|
|
</ul>
|
|
</p>
|
|
</div>
|
|
|
|
|
|
</div></div></div></body></html> |