★ wanayoo — archive 1999 http://www.php.net/manual/ja/function.sprintf.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to 文字列
Quick Reference
English version of this pageGerman version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
文字列
* AddCSlashes
* AddSlashes
* bin2hex
* Chop
* Chr
* chunk_split
* convert_cyr_string
* count_chars
* crc32
* crypt
* echo
* explode
* flush
* get_html_translation_table
* get_meta_tags
* hebrev
* hebrevc
* htmlentities
* htmlspecialchars
* implode
* join
* levenshtein
* ltrim
* md5
* Metaphone
* nl2br
* ob_start
* ob_get_contents
* ob_end_flush
* ob_end_clean
* ob_implicit_flush
* Ord
* parse_str
* print
* printf
* quoted_printable_decode
* QuoteMeta
* rtrim
* sscanf
* setlocale
* similar_text
* soundex
* sprintf
* strcasecmp
* strchr
* strcmp
* strcspn
* strip_tags
* StripCSlashes
* StripSlashes
* stristr
* strlen
* str_pad
* strpos
* strrchr
* str_repeat
* strrev
* strrpos
* strspn
* strstr
* strtok
* strtolower
* strtoupper
* str_replace
* strtr
* substr
* substr_count
* substr_replace
* trim
* ucfirst
* ucwords
Manual: sprintf
View the source code for this pageSearch the site



Previous page
 soundex
 Updated
Mon, 14 Aug 2000
strcasecmp 
Next page


sprintf

(PHP3 , PHP4 )

sprintf -- フォーマットされた文字列を返す

説明

sprintf (string format [, mixed args...])

フォーマット文字列formatにしたがって 生成された文字列を返します。

フォーマット文字列は0個以上のディレクティブ(指示子)により 構成されます。ディレクティブには、そのまま結果にコピーされる (%を除く)通常の文字と変換指定子 (conversion specifications)があり、取り出される際は どちらもそれ自身がパラメータとなります。このことは sprintf()の場合だけでなくprintf() の場合も同様です。

各変換指定子は3つの要素から成ります。これらを順に説明して いきます。

  1. オプションのパディング指定子。これは、 文字列が正しい長さになるまでどんな文字で埋めるかということを 指定します。これは空白かまたは0(文字'0') のいずれかです。デフォルトでは空白で埋められます。 これ以外のパディング文字を指定するには、その文字の前に単一 引用符(')を置きます。後述の例を参照して ください。

  2. オプションのアラインメント指定子。これは 結果を左寄せまたは右寄せにしたい場合に指定します。デフォルトは 右寄せです。ここで-文字を指定すると左寄せ となります。

  3. オプションの数字。これは表示幅指定子です。 結果を(最低)何桁にするかを指定します。

  4. オプションの精度指定子。これは、浮動小数点 数に対して何個の数字を表示するかを指定します。このオプションは、 double(倍精度)型以外には何の効果もありません。 (数字をフォーマットする際に便利な関数として他に number_format() があります。)

  5. 型指定子。引数を何の型として扱うかを指定 します。指定できる型を以下に示します。

    % - パーセント文字。引数は不要です。
    b - 引数を整数として扱い、バイナリの数値 として表現します。
    c - 引数を整数として扱い、その ASCII 値 の文字として表現します。
    d - 引数を整数として扱い、10 進数として 表現します。
    f - 引数を double として扱い、浮動小数点数 として表現します。
    o - 引数を整数として扱い、8 進数として 表現します。
    s - 引数を文字列として扱い、表現します。
    x - 引数を整数として扱い、16 進数として (小文字で)表現します。
    X - 引数を整数として扱い、16 進数として (大文字で)表現します。

printf(), number_format() も参照下さい。

例 1. sprintf: 整数を0でパディング


$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
      

例 2. sprintf: 貨幣をフォーマットする例


$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money は "123.1" を出力します。
$formatted = sprintf ("%01.2f", $money);
// echo $formatted は "123.10"を出力します
      


User Contributed Notes: sprintf


paolini@kahuna.sdsu.edu
14-Apr-1999 01:12
Hey folks, don't forget to prefix a precision specifier with a period '.'!
Thus, to print a floating point number,
say $x, with two digits after the decimal point you would write:

<PRE>
printf( "%.2f", $x );
</PRE>



guenther.mair@web.by.com
07-Jul-1999 12:37
to add a percent sign, substitute every % with %% ;)


kris@mha.ca
21-Sep-1999 07:27
Left justify sprintf:

<PRE> sprintf("%-100s",$txt);</PRE>

Kris



chaizzilla@hotmail.com
20-Oct-1999 03:03
does sprintf() have a means to also determine the maximum charachters the conversion results in? i'm trying to ditch tables.. "3.An optional number, a width specifier that says how many characters (minimum) this conversion should result in."


robertk@robertk.com
20-Nov-1999 12:37
Here's a function which will format numbers with commas where necessary, and optionally add a dollar sign.

<pre>
function Money($inbuffer, $dolsign = 0)
{
$buffer = sprintf("%0.2f", $inbuffer);
if(strlen($buffer) > 6)
{
$thenumber = "";
$decimal = substr($buffer, strrpos($buffer, "."));
$left = substr($buffer, 0, strrpos($buffer, "."));
while(strlen($left) > 3)
{
$thenumber = $thenumber . "," . substr($left, -3);
$left = substr($left, 0, strlen($left)-3);
}
$thenumber = $left . $thenumber . $decimal;
}
else
$thenumber = $buffer;
if($dolsign != 0)
$thenumber = "\$ $thenumber";
return($thenumber);
}
</pre>



scott@mha.ca
01-Dec-1999 05:38
if you want to format a date, you should NOT be using sprintf(), you should be using date() ..


bschuetz@affinity.net
03-Dec-1999 02:59
Actually if you want to format numbers with commas a much easier method would be to just use the number_format() function.


dean@av8.com
09-Feb-2000 11:12
I need to format an unsigned number for a sql query eg sprintf("select servername from x where ip > '%d' and ip < '%d'", 130<<24|105<<16|11<<8, 130<<24|105<<16|11<<8|255);
in php3: the value is printed as -2107044865

perl -e 'print 130<<24|105<<16|11<8|255'
2187919615

What to do? Don't want to rewrite everything in Java... Really __need__ to have unsigned numbers...



olip@xzone.pl
21-Feb-2000 12:39
if U need counter do it like that:

printf("%07d",$counter_value);

U will get eg. 0000005 or 0000150 dependign on the $counter_value

If u wanna have counter longer or shorter change the 7 to whatever U need.



tim_converse@excite.com
11-Jul-2000 02:00
Conversion specifications begin with a percent sign (%). (This is obvious from the examples, but the explanation does not say so directly.)



alec@brainpod.com
01-Aug-2000 01:11
This is my function for converting mysql dates into dates to be passed to the php date function...

<pre>
cfunction mysqlDate($date) {
ereg("(....)(..)(..)(..)(..)(..)",$date,$reg);
return mktime($reg[4],$reg[5],$reg[6],$reg[2],$reg[3],$reg[1]);
}

echo date("M d Y",mysqlDate($somedb->f('datefield'));
</pre>


This is a good function to keep in all your db code..



 About Notes


Previous page
 soundex
 Updated
Mon, 14 Aug 2000
strcasecmp 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.