cvs: phpdoc-da /reference/strings/functions sprintf.xml
[email protected] ("Jonathan Holst")
| Newsgroups | php.doc.da |
|---|---|
| Message-ID | <cvsholst1090590086@cvsserver> |
holst Fri Jul 23 09:41:26 2004 EDT
Added files:
/phpdoc-da/reference/strings/functions sprintf.xml
Log:
Initial translation
holst-20040723094126.txt
(text/plain, 8.7 KB)
http://cvs.php.net/co.php/phpdoc-da/reference/strings/functions/sprintf.xml?r=1.1&p=1
Index: phpdoc-da/reference/strings/functions/sprintf.xml
+++ phpdoc-da/reference/strings/functions/sprintf.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<!-- EN-Revision: 1.9 Maintainer: holst Status: ready -->
<refentry id="function.sprintf">
<refnamediv>
<refname>sprintf</refname>
<refpurpose>Returner en formateret streng</refpurpose>
</refnamediv>
<refsect1>
<title>Beskrivelse</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>
</methodsynopsis>
<simpara>
Returnerer en streng produceret af specifikationerne givet i
<parameter>format</parameter>.
</simpara>
<simpara>
Formateringsstrengen bliver dannet af ingen eller flere direktiver:
ordinære tegn (eksklusiv <literal>%</literal>) som bliver kopieret
direkte til resultatet og
<emphasis>konverteringsspecifikationer</emphasis>, hvert resulterer i at
fange dets eget parameter. Dette gælder for både
<function>sprintf</function> og <function>printf</function>.
The format string is composed of zero or more directives:
ordinary characters (excluding <literal>%</literal>) that are
copied directly to the result, and <emphasis>conversion
specifications</emphasis>, each of which results in fetching its
own parameter. This applies to both <function>sprintf</function>
and <function>printf</function>.
</simpara>
<para>
Hver konverteringspecifikation består af et procenttegn
(<literal>%</literal>), fulgt af et eller flere af disse elementer, i
rækkefølge:
<orderedlist>
<listitem>
<simpara>
En valgfri <emphasis>padding specifier</emphasis> som fortæller
hvilken karakter der skal bruges til at udfylde resultaterne til den
korrekte strengstørrelse. Der kan bruges et mellemrum eller et
<literal>0</literal> (nultegn). Standarden er at udfylde med
mellemrum. Et alternativ udfyldningstegn kan blive specificeret ved
præfikse dem med et enkelt citationstegn (<literal>'</literal>). Se
eksemplerne herunder.
</simpara>
</listitem>
<listitem>
<simpara>
En valgfri<emphasis>alignment specifier</emphasis> som siger om
resultatet skal være venstrestillet eller højrestillet.
Standarden er højrestillet; et <literal>-</literal> tegn her vil gøre
det venstrestillet.
</simpara>
</listitem>
<listitem>
<simpara>
Et valgfrit tal, en <emphasis>width specifier</emphasis> som fortæller
hvor mange tegn (minimum) denne konvertering skal resultere i.
</simpara>
</listitem>
<listitem>
<simpara>
En valgfri <emphasis>precision specifier</emphasis> som fortæller hvor
mange decimalcifre der skal vises som flydende decimalnumre. Denne
mulighed har ikke nogen effekt for andre typer end <type>float</type>.
(En anden funktion der er brugbar for formatering af numre er
<function>number_format</function>).
</simpara>
</listitem>
<listitem>
<para>
A <emphasis>type specifier</emphasis> that says what type the
argument data should be treated as. Possible types:
<simplelist>
<member>
<literal>%</literal> - a literal percent character. No
argument is required.
</member>
<member>
<literal>b</literal> - the argument is treated as an
integer, and presented as a binary number.
</member>
<member>
<literal>c</literal> - the argument is treated as an
integer, and presented as the character with that ASCII
value.
</member>
<member>
<literal>d</literal> - the argument is treated as an
integer, and presented as a (signed) decimal number.
</member>
<member>
<literal>e</literal> - the argument is treated as scientific
notation (e.g. 1.2e+2).
</member>
<member>
<literal>u</literal> - the argument is treated as an
integer, and presented as an unsigned decimal number.
</member>
<member>
<literal>f</literal> - the argument is treated as a
<type>float</type>, and presented as a floating-point number.
</member>
<member>
<literal>o</literal> - the argument is treated as an
integer, and presented as an octal number.
</member>
<member>
<literal>s</literal> - the argument is treated as and
presented as a string.
</member>
<member>
<literal>x</literal> - the argument is treated as an integer
and presented as a hexadecimal number (with lowercase
letters).
</member>
<member>
<literal>X</literal> - the argument is treated as an integer
and presented as a hexadecimal number (with uppercase
letters).
</member>
</simplelist>
</para>
</listitem>
</orderedlist>
</para>
<para>
As of PHP 4.0.6 the format string supports argument
numbering/swapping. Here is an example:
<example>
<title>Argument swapping</title>
<programlisting role="php">
<![CDATA[
<?php
$format = "There are %d monkeys in the %s";
printf($format, $num, $location);
?>
]]>
</programlisting>
</example>
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:
<example>
<title>Argument swapping</title>
<programlisting role="php">
<![CDATA[
<?php
$format = "The %s contains %d monkeys";
printf($format, $num, $location);
?>
]]>
</programlisting>
</example>
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:
<example>
<title>Argument swapping</title>
<programlisting role="php">
<![CDATA[
<?php
$format = "The %2\$s contains %1\$d monkeys";
printf($format, $num, $location);
?>
]]>
</programlisting>
</example>
An added benefit here is that you can repeat the placeholders without
adding more arguments in the code. For example:
<example>
<title>Argument swapping</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>
See also <function>printf</function>,
<function>sscanf</function>, <function>fscanf</function>,
<function>vsprintf</function>, and
<function>number_format</function>.
</simpara>
</refsect1>
<refsect1>
<title>Examples</title>
<para>
<example>
<title><function>sprintf</function>: zero-padded integers</title>
<programlisting role="php">
<![CDATA[
<?php
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
?>
]]>
</programlisting>
</example>
<example>
<title><function>sprintf</function>: formatting currency</title>
<programlisting role="php">
<![CDATA[
<?php
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "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
-->