1
0
mirror of https://github.com/php/doc-ja.git synced 2026-04-29 02:53:11 +02:00
Files
archived-doc-ja/reference/strings/functions/sprintf.xml
T
Rui Hirokawa 2e4f8a3165 sync with en.
git-svn-id: https://svn.php.net/repository/phpdoc/ja/trunk@186046 c90b9560-bf6c-de11-be94-00142212c4b1
2005-05-08 23:41:35 +00:00

341 lines
11 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.5 $ -->
<!-- EN-Revision: 1.15 Maintainer: hirokawa Status: working -->
<refentry id="function.sprintf">
<refnamediv>
<refname>sprintf</refname>
<refpurpose>フォーマットされた文字列を返す</refpurpose>
</refnamediv>
<refsect1>
<title>説明</title>
<methodsynopsis>
<type>string</type><methodname>sprintf</methodname>
<methodparam><type>string</type><parameter>format</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>args</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>...</parameter></methodparam>
</methodsynopsis>
<simpara>
フォーマット文字列<parameter>format</parameter>に基づき生成された
文字列を返します。
</simpara>
<simpara>
フォーマット文字列は0個以上のディレクティブ(指示子)により
構成されます。ディレクティブには、そのまま結果にコピーされる
(<literal>%</literal>を除く)通常の文字と<emphasis>変換指定子
(conversion specifications)</emphasis>があり、取り出される際は
どちらもそれ自身がパラメータとなります。このことは<function>
sprintf</function>の場合だけでなく<function>printf</function>
の場合も同様です。
</simpara>
<para>
各変換指定子はパーセント記号(<literal>%</literal>)の後に、これら
の要素が一つ以上続いたものになります。
<orderedlist>
<listitem>
<simpara>
An optional <emphasis>sign specifier</emphasis> 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.
</simpara>
<simpara>
オプションの<emphasis>パディング指定子</emphasis>。これは、
文字列が正しい長さになるまでどんな文字で埋めるかということを
指定します。これは空白かまたは<literal>0</literal>(文字'0')
のいずれかです。デフォルトでは空白で埋められます。
これ以外のパディング文字を指定するには、その文字の前に単一
引用符(<literal>'</literal>)を置きます。後述の例を参照して
ください。
</simpara>
</listitem>
<listitem>
<simpara>
オプションの<emphasis>アラインメント指定子</emphasis>。これは
結果を左寄せまたは右寄せにしたい場合に指定します。デフォルトは
右寄せです。ここで<literal>-</literal>文字を指定すると左寄せ
となります。
</simpara>
</listitem>
<listitem>
<simpara>
オプションの数字。これは<emphasis>表示幅指定子</emphasis>です。
結果を(最低)何桁にするかを指定します。
</simpara>
</listitem>
<listitem>
<simpara>
オプションの<emphasis>精度指定子</emphasis>。これは、浮動小数点
数に対して何個の数字を表示するかを指定します。
When using this specifier on a string, it acts as a
cutoff point, setting a maximum character limit to the string.
</simpara>
</listitem>
<listitem>
<para>
<emphasis>型指定子</emphasis>。引数を何の型として扱うかを指定
します。指定できる型を以下に示します。
<simplelist>
<member>
<literal>%</literal> - パーセント文字。引数は不要です。
</member>
<member>
<literal>b</literal> - 引数を整数として扱い、バイナリの数値
として表現します。
</member>
<member>
<literal>c</literal> - 引数を整数として扱い、その ASCII 値
の文字として表現します。
</member>
<member>
<literal>d</literal> - 引数を整数として扱い、10 進数として
表現します。
</member>
<member>
<literal>e</literal> - the argument is treated as scientific
notation (e.g. 1.2e+2).
</member>
<member>
<literal>u</literal> - 引数を整数として扱い、符号無しの10進
数として表現します。
</member>
<member>
<literal>f</literal> - 引数を double として扱い、浮動小数点数
として表現します。
</member>
<member>
<literal>F</literal> - 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.
</member>
<member>
<literal>o</literal> - 引数を整数として扱い、8 進数として
表現します。
</member>
<member>
<literal>s</literal> - 引数を文字列として扱い、表現します。
</member>
<member>
<literal>x</literal> - 引数を整数として扱い、16 進数として
(小文字で)表現します。
</member>
<member>
<literal>X</literal> - 引数を整数として扱い、16 進数として
(大文字で)表現します。
</member>
</simplelist>
</para>
</listitem>
</orderedlist>
</para>
<para>
PHP4.0.6以降でフォーマット文字列で引数の番号付け/交換が
サポートされました。以下に例を示します。
<example>
<title>引数の交換</title>
<programlisting role="php">
<![CDATA[
<?php
$format = "There are %d monkeys in the %s";
printf($format,$num,$location);
?>
]]>
</programlisting>
</example>
この出力は、"There are 5 monkeys in the tree" のようになります。
ここで、フォーマット文字列が別のファイルにある場合を考えてみましょ
う。これは、出力を国際化したりする場合に行われる可能性があります。
この場合、次の書き変えられます。
<example>
<title>引数の交換</title>
<programlisting role="php">
<![CDATA[
<?php
$format = "The %s contains %d monkeys";
printf($format, $num, $location);
?>
]]>
</programlisting>
</example>
ここで、問題が発生します。フォーマット文字列における置換指示子の
順番は、コードにおける引数の順番と一致していません。コードは変更
せず、置換指示子が参照するフォーマット文字列で指示を行う方が望ま
しいと言えます。フォーマット文字列を次のように書き換えてみましょ
う。
<example>
<title>引数の交換</title>
<programlisting role="php">
<![CDATA[
<?php
$format = "The %2\$s contains %1\$d monkeys";
printf($format, $num, $location);
?>
]]>
</programlisting>
</example>
加えて、これによりコードに引数を追加せずに置換指示子を複数回使用
することも可能になります。例えば、次のようになります。
<example>
<title>引数の交換</title>
<programlisting role="php">
<![CDATA[
<?php
$format = "The %2\$s contains %1\$d monkeys.
That's a nice %2\$s full of %1\$d monkeys.";
printf($format, $num, $location);
?>
]]>
</programlisting>
</example>
</para>
<simpara>
<function>printf</function>,
<function>sscanf</function>, <function>fscanf</function>,
<function>vsprintf</function>,
<function>number_format</function>
も参照ください。
</simpara>
</refsect1>
<refsect1>
<title></title>
<para>
<example>
<title><function>printf</function>: various examples</title>
<programlisting role="php">
<![CDATA[
<?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
?>
]]>
</programlisting>
<para>
The printout of this program would be:
</para>
<screen>
<![CDATA[
%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'
]]>
</screen>
</example>
<example>
<title><function>printf</function>: string specifiers</title>
<programlisting role="php">
<![CDATA[
<?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
?>
]]>
</programlisting>
<para>
The printout of this program would be:
</para>
<screen>
<![CDATA[
[monkey]
[ monkey]
[monkey ]
[0000monkey]
[####monkey]
[many monke]
]]>
</screen>
</example>
<example>
<title>sprintf: 整数を0でパディング</title>
<programlisting role="php">
<![CDATA[
<?php
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
?>
]]>
</programlisting>
</example>
<example>
<title>sprintf: 通貨をフォーマットする例</title>
<programlisting role="php">
<![CDATA[
<?php
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money は "123.1" を出力します。
$formatted = sprintf ("%01.2f", $money);
// echo $formatted は "123.10"を出力します
?>
]]>
</programlisting>
</example>
<example>
<title><function>sprintf</function>: scientific notation</title>
<programlisting role="php">
<![CDATA[
<?php
$number = 362525200;
echo sprintf("%.3e", $number); // outputs 3.63e+8
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->