There are quite a few I notice but I have no clue what they are used for, such as sprintf, vsprintf etc.. I had used the basic formatting chars like %c, %s and %d in C but what are the others?
String formatting functions?
Started by FireGator, Sep 10 2010 06:02 AM
5 replies to this topic
#1
Posted 10 September 2010 - 06:02 AM
|
|
|
#2
Posted 10 September 2010 - 06:30 AM
There are many types according to the manual; a quote (omitting some for length):
The functions are defined as followed, I'll show examples of some to show the usefulness of them:
printf(): output a formatted string
sprintf: return a formatted string:
users.txt:
vsprintf/vprintf(): return/print formatted string, but with array as arguments:
"php.net/manual" said:
% - 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). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of significant digits (one less).
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).
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).
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). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of significant digits (one less).
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).
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).
The functions are defined as followed, I'll show examples of some to show the usefulness of them:
printf(): output a formatted string
sprintf: return a formatted string:
$query = sprintf("SELECT %s FROM %s WHERE %s < %d", $date, 'tbl_foo', 'date', 41020102);sscanf(): parses input of a string according to format:list($m, $d, $y) = sscanf("02/30/05", "%d/%d/%d");Note: You can do some tricky things as you can in C, such as://format to parse line of Apache access log: $apacheformat = '%s %s %s [%[^]]] "%s %s %[^"]" %d %s "%[^"]" "%[^"]"';fscanf(): parses input of a given file:
users.txt:
john 23 admin tom 51 user jim 19 contributorusers.php:
$handle = fopen("users.txt", "r");
while ($userinfo = fscanf($handle, "%s\t%s\t%s\n")) {
list ($name, $age, $title) = $userinfo;
print "$name - $age - $title<br/>";
}
fclose($handle);vsprintf/vprintf(): return/print formatted string, but with array as arguments:
$userarguments = $john->dump('id', 'data', 'auth');
$formatted = vsprintf('ID: %s, Data: %s, Auth: %s', $userarguments);
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 10 September 2010 - 07:20 AM
Most of the time, you don't need to use this in php if you don't want to. Concatenation (the dot operator) and string parsing makes a lot of it obsolete, but not all of it.
in C, you do this:
most use of it is when to format numbers...
in C, you do this:
printf("That was a %s car", color);
but in php, you can do this echo "That was a $color car";or
echo "That was a ".$color." car";
most use of it is when to format numbers...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#4
Posted 10 September 2010 - 07:29 AM
Along with Orjan's post, you should note that concatination is partially faster than calling printf, although I am not sure the awkward situation you would need to use many prints (say more than 100). PHP supports HEREDOC/NOWDOC syntax as well but that is quite slow, so I would stay away from that.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#5
Posted 10 September 2010 - 08:19 AM
You guys came to the rescue again! I'm sure glad to have these resources, thank you guys again very much.
Nullw0rm, you mentioned that concatination is faster than printf, how much is this so?
Nullw0rm, you mentioned that concatination is faster than printf, how much is this so?
#6
Posted 10 September 2010 - 10:39 AM
FireGator said:
Nullw0rm, you mentioned that concatination is faster than printf, how much is this so?
Well remember in the end it's just an interpreted language. Single quotes are fastest, as there is no need to parse for tokens. Technically 'foo' . $bar . 'baz' is the fastest, as it knows when to start and stop parsing the string, same as: "{$array['key']}" is faster than "$array[key]", there is no need to check ifdefined on the given token.
If you were silly and benchmark 10 million prints, you may get double quotes with embedded token variables to be 20% slower, but remember that 1/10000000th of 1.2s is too small for anybody to care about when using it in script!
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.


Sign In
Create Account


Back to top









