Website Development Prices

Search Blog

Price za decu

Monday, February 1, 2016

Formatiranje stringova (Formatting strings)

Dve funkcije za stringove kada zelite da formatirate podatke radi prikazivanja (npr. ako zelite da brojeve formatirate u formi stringa). To su printf i sprintf. Funkcija printf direktno prikazuje tekst. Povratnu vrednost funkcije sprintf mozete da pridruzite promenljivoj.

Primer:

printf(format [, args])
sprintf(format [, args]) 

Objasnjenje: stavke su uglastim zagradama [] i one su opcione; format stringa se sastoji od jedne ili vise direktiva, karaktera koji se kopira direktno u rezultat i specifikacije konverzije. Svaka specifikacija konverzije sastoji se od znaka procenta (%), iza kojeg slede jedan ili vise elemenata:


  • opcioni specifikator dopune, koji ukazuje koji se karakter koristi za dopunu rezultata do prave velicine. To mogu biti blanko karakter ili nula. Podrazumevana je dopuna blanko karakterima.  
  • opcioni specifikator ravnjanja, koji ukazuje da li rezultat treba da bude levo ili desno poravnat. Podrazumevano je desno ravnjanje (karakter - znaci da ce ravnjanje biti levo).
  • opcioni broj, specifikator sirine, koji definise koliko karaktera (minimalno) ova konverzija treba da obezbedi. 
  • opcioni specifikator preciznosti, koji ukazuje koliko decimalnih cifara treba da bude prikazano kod realnih brojeva (ovo ne utice na druge tipove podataka).
  • specifikator tipa, koji ukazuje kako treba da se tretira ulazni podataka.


Moguci tipovi specifikatora tipa:

%   procenat. Nije potreban argument.
b    argument se tretira kao integer, a predstavljen je kao binarni broj.c    argument se tretira kao integer, a predstavljen je kao karakter sa ASCII vrednoscu.
d    argument se tretira kao integer, a predstavljen je kao decimalni broj.
u    argument se tretira kao integer, a predstavljen je kao neoznacen decimalni broj.
f    argument se tretira kao realan broj, a predstavljen je kao realan broj.
-    argument se tretira kao integer, a predstavlja se kao oktalni broj.
s    argument se tretira kao integer, a predstavlja string.
x    argument se tretira kao integer, a predstavlja se kao heksadecimalni broj (sa malim slovima).
X    argument se tretira kao integer, a predstavlja se kao heksadecimalni broj (sa velikim slovima).

Specifikator formata %5.2, znaci da konkretni realan broj treba da ima pet mesta za prikazivanje, od kojih dva su za decimalnu tacku. 

Primer:

<?php

printf("Ja imam %s kruske i %s banana.<br>", 4, 56);

$godina = 2016;
$mesec = 2;
$dan = 1;
printf("%04d-%02d-%02d<br />", $godina, $mesec, $dan);

$cena = 3999.99;
printf("\$%01.2f<br>", $cena);

$string = sprintf("A sada imam %s kruske i %s banana.<br>", 3, 15);
echo $string;

?> 

Rezultat:

Ja imam 4 kruske i 56 banana.
2016-02-01
$3999.99
A sada imam 3 kruske i 15 banana.


Two functions for strings when you want to reformat the data to display (for example, if you want to format the numbers in the form of a string). These are the printf and sprintf. Function printf directly displays text. A return value of the function sprintf you can join to variable.

Example:

printf (format [, args])
sprintf (format [, args])

Explanation: items are in square brackets [] and are optional; format string consists of one or more directives, the character of which is copied directly to the result, and conversion specification. Each conversion specification consists of a percent sign (%), followed by one or more elements:

  • optional supplement specifier, indicating that the character used to supplement the results to the right size. This can be a blank character or a zero. The default is to supplement blank characters.
  • optional alignment specifier, which indicates whether the result should be left or right aligned. By default, is the right alignment (character - means that the alignment will be left).
  • optional number, a width specifier, which defines how many characters (minimum) this conversion should provide.
  • optional precision specifier, which indicates how many decimal digits should be displayed at the real numbers (this does not affect other types of data).
  • specifier type, which indicates how they should treat input data.


Possible types of specifiers type:

%    percentage. It is not required argument.
b     argument is treated as an integer, and presented as a binary number.
c     argument is treated as an integer, and presented as a character with an ASCII value.
d    argument is treated as an integer, and presented as a decimal number.
u    argument is treated as an integer, and presented as an unsigned decimal number.
f     argument is treated as a real number, and is presented as a real number.
-     argument is treated as an integer, and presented as an octal number.
s    argument is treated as an integer, represents string.
x    argument is treated as an integer, and presented as a hexadecimal number (with lowercase).
X    argument is treated as an integer, and presented as a hexadecimal number (with uppercase letters).

Format specifier 5.2%, means that specific real number should have five places to display, two of which are for a decimal point.

Example:

<?php

printf("I have %s pears and %s bananas.<br>", 4, 56);

$year = 2016;
$month = 2;
$day = 1;
printf("%04d-%02d-%02d<br />", $year, $month, $day);

$price = 3999.99;
printf("\$%01.2f<br>", $price);

$string = sprintf("And now I have %s pears and %s bananas.<br>", 3, 15);
echo $string;


?>

Result:

I have 4 pears and 56 bananas.
2016-02-01
$3999.99
And now I have 3 pears and 15 bananas.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.