|
użytkowników online: 48
|
OPINIE UŻYTKOWNIKÓW
|
Na początku, kiedy zobaczyłem, że ktoś chce jakiejś opłaty za pomoc w tworzeniu stron ryknąłem śmiechem - potem przyszły problemy... i zaryzykowałem. Druga rzecz to: nie chciałem "kopiować". Ale prawda jest taka: są lepsi, bardziej doświadczeni i... czasem trzeba poprosić o pomoc, a jak poświęca się na to trzecią cześć życia, to nic dziwnego, że nie chce się swoich "sekretów" zdradzać za darmo. Skorzystałem z "algorytmy.pl" i naprawdę jestem z tego w 100% zadowolony, polecam - dla zawodowców (co się uczą) i amatorów (można skorzystać z gotowego rozwiązania).
Tomasz Czypicki
Cybernoxa
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
sprintf (PHP 3, PHP 4, PHP 5) sprintf -- Return a formatted string Descriptionstring sprintf ( string format [, mixed args [, mixed ...]] )
Returns a string produced according to the formatting string
format.
The format string is composed of zero or more directives:
ordinary characters (excluding %) that are
copied directly to the result, and conversion
specifications, each of which results in fetching its
own parameter. This applies to both sprintf()
and printf().
Each conversion specification consists of a percent sign
(%), followed by one or more of these
elements, in order:
An optional sign specifier 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.
An optional padding specifier that says
what character will be used for padding the results to the
right string size. This may be a space character or a
0 (zero character). The default is to pad
with spaces. An alternate padding character can be specified
by prefixing it with a single quote (').
See the examples below.
An optional alignment specifier that says
if the result should be left-justified or right-justified.
The default is right-justified; a -
character here will make it left-justified.
An optional number, a width specifier
that says how many characters (minimum) this conversion should
result in.
An optional precision specifier 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.
A type specifier that says what type the
argument data should be treated as. Possible types:
|
% - a literal percent character. No
argument is required.
| |
b - the argument is treated as an
integer, and presented as a binary number.
| |
c - the argument is treated as an
integer, and presented as the character with that ASCII
value.
| |
d - the argument is treated as an
integer, and presented as a (signed) decimal number.
| |
e - the argument is treated as scientific
notation (e.g. 1.2e+2).
| |
u - the argument is treated as an
integer, and presented as an unsigned decimal number.
| |
f - the argument is treated as a
float, and presented as a floating-point number (locale aware).
| |
F - 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.
| |
o - the argument is treated as an
integer, and presented as an octal number.
| |
s - the argument is treated as and
presented as a string.
| |
x - the argument is treated as an integer
and presented as a hexadecimal number (with lowercase
letters).
| |
X - the argument is treated as an integer
and presented as a hexadecimal number (with uppercase
letters).
|
As of PHP 4.0.6 the format string supports argument
numbering/swapping. Here is an example:
Przykład 1. Argument swapping |
<?php
$format = "There are %d monkeys in the %s";
printf($format, $num, $location);
?>
|
|
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:
Przykład 2. Argument swapping |
<?php
$format = "The %s contains %d monkeys";
printf($format, $num, $location);
?>
|
|
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:
Przykład 3. Argument swapping |
<?php
$format = "The %2\$s contains %1\$d monkeys";
printf($format, $num, $location);
?>
|
|
An added benefit here is that you can repeat the placeholders without
adding more arguments in the code. For example:
Przykład 4. Argument swapping |
<?php
$format = "The %2\$s contains %1\$d monkeys.
That's a nice %2\$s full of %1\$d monkeys.";
printf($format, $num, $location);
?>
|
|
See also printf(),
sscanf(), fscanf(),
vsprintf(), and
number_format().
ExamplesPrzykład 5. printf(): various examples |
<?php
$n = 43951789;
$u = -43951789;
$c = 65; printf("%%b = '%b'\n", $n); printf("%%c = '%c'\n", $c); printf("%%d = '%d'\n", $n); printf("%%e = '%e'\n", $n); printf("%%u = '%u'\n", $n); printf("%%u = '%u'\n", $u); printf("%%f = '%f'\n", $n); printf("%%o = '%o'\n", $n); printf("%%s = '%s'\n", $n); printf("%%x = '%x'\n", $n); printf("%%X = '%X'\n", $n); printf("%%+d = '%+d'\n", $n); printf("%%+d = '%+d'\n", $u); ?>
|
The printout of this program would be:
%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' |
|
Przykład 6. printf(): string specifiers |
<?php
$s = 'monkey';
$t = 'many monkeys';
printf("[%s]\n", $s); printf("[%10s]\n", $s); printf("[%-10s]\n", $s); printf("[%010s]\n", $s); printf("[%'#10s]\n", $s); printf("[%10.10s]\n", $t); ?>
|
The printout of this program would be:
[monkey]
[ monkey]
[monkey ]
[0000monkey]
[####monkey]
[many monke] |
|
Przykład 7. sprintf(): zero-padded integers |
<?php
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
?>
|
|
Przykład 8. sprintf(): formatting currency |
<?php
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
$formatted = sprintf("%01.2f", $money);
?>
|
|
Przykład 9. sprintf(): scientific notation |
<?php
$number = 362525200;
echo sprintf("%.3e", $number); ?>
|
|
User Contributed NotesdarkfalconIV at hotmail dot com
18-Dec-2005 09:57
henke dot andersson
You can accomplish feeding it array if you use call_user_func_array. Not exactly a `clean' option, but it does work.
jonybd
14-Nov-2005 03:02
Time ? Format
<?php
$v_Dur = "66";
$v_Dur = floor($v_Dur/60) . ":" . number_format( fmod(($v_Dur/60)*60,60) ) ;
echo "HH:MM========" . $v_Dur;
echo "<br>";
$v_Dur = "66";
$v_Dur = floor($v_Dur/60) . ":" . sprintf("%02s",number_format( fmod(($v_Dur/60)*60,60) ) );
echo "HH:MM========" . $v_Dur;
?>
tim dot brouckaert dot NOSPAM at gmail dot com
12-Oct-2005 02:35
If you want to center align some text using the printf or sprintf functions, you can just use the following:
function center_text($word){
$tot_width = 30;
$symbol = "-";
$middle = round($tot_width/2);
$length_word = strlen($word);
$middle_word = round($length_word / 2);
$last_position = $middle + $middle_word;
$number_of_spaces = $middle - $middle_word;
$result = sprintf("%'{$symbol}{$last_position}s", $word);
for ($i = 0; $i < $number_of_spaces; $i++){
$result .= "$symbol";
}
return $result;
}
$string = "This is some text";
print center_text($string);
off course you can modify the function to use more arguments.
webmaster at cafe-clope dot net
15-Aug-2005 06:47
trying to fix the multibyte non-compliance of sprintf, I came to that :
<?php
function mb_sprintf($format) {
$argv = func_get_args() ;
array_shift($argv) ;
return mb_vsprintf($format, $argv) ;
}
function mb_vsprintf($format, $argv) {
$newargv = array() ;
preg_match_all("`\%('.+|[0 ]|)([1-9][0-9]*|)s`U", $format, $results, PREG_SET_ORDER) ;
foreach($results as $result) {
list($string_format, $filler, $size) = $result ;
if(strlen($filler)>1)
$filler = substr($filler, 1) ;
while(!is_string($arg = array_shift($argv)))
$newargv[] = $arg ;
$pos = strpos($format, $string_format) ;
$format = substr($format, 0, $pos)
. ($size ? str_repeat($filler, $size-strlen($arg)) : '')
. str_replace('%', '%%', $arg)
. substr($format, $pos+strlen($string_format))
;
}
return vsprintf($format, $newargv) ;
}
?>
handle with care :
1. that function was designed mostly for utf-8. i guess it won't work with any static mb encoding.
2. my configuration sets the mbstring.func_overload configuration directive to 7, so you may wish to replace substr, strlen, etc. with mb_* equivalents.
3. since preg_* doesn't complies with mb strings, I used a '.+' in the regexp to symbolize an escaped filler character. That means, %'xy5s pattern will match, unfortunately. It is recomended to remove the '+', unless you are intending to use an mb char as filler.
4. the filler fills at left, and only at left.
5. I couldn't succeed with a preg_replace thing : the problem was to use the differents lengths of the string arguements in the same replacement, string or callback. That's why the code is much longuer than I expected.
6. The pattern wil not match any %1\$s thing... just was too complicated for me.
7. Although it has been tested, and works fine within the limits above, this is much more a draft than a end-user function. I would enjoy any improvment.
The test code below shows possibilities, and explains the problem that occures with an mb string argument in sprintf.
<?php
header("content-type:text/plain; charset=UTF-8") ;
$mb_string = "x
|