Printf and sscanf examples

This example shows how to use the printf() and sscanf() functions.
(You can also replace printf() with sprintf() to return a result instead of printing it).

More detailed informations: php.net

$file = "test.txt"; $lines = 7;
printf("The file %s consists of %d lines\n", $file, $lines);
// returns --> The file test.txt consists of 7 lines
 
 
// padding something, prefix a string with "_"
$word = 'foobar';
printf("%'_10s\n", $word);
// returns --> ____foobar
 
 
// format a number:
$number = 100.85995;
printf("%03d\n", $number); // returns --> 100
printf("%01.2f\n", $number); // returns --> 100.86
printf("%01.3f\n", $number); // returns --> 100.860
 
 
// parse a string with sscanf #1
list($number) = sscanf("ID/1234567","ID/%d");
print "$number\n";
// returns --> 1234567
 
 
// parse a string with sscanf #2
$test = "string 1234 string 5678";
$result = sscanf($test, "%s %d %s %d");
 
print_r($result);
 
/*
 
--> returns:
 
Array
(
    [0] => string
    [1] => 1234
    [2] => string
    [3] => 5678
)
 
*/
Snippet Details




Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

None.