In PHP programming, sprintf is a very powerful function that allows you to format variable values into strings based on a specific format. In development, we often need to format the output, such as the display format of numbers, the output method of dates, etc. At this time, sprintf can play a role. This article will take you into the deeper understanding of the sprintf function and its common formatted placeholders.
printf and sprintf functions are both tools used to format output in PHP, but they have some differences. printf directly outputs the formatted string, while sprintf returns the formatted string. Specifically, the syntax of sprintf is as follows:
sprintf(string $format, mixed ...$values): string
$format : Format string, containing formatted placeholders.
$values : A set of variables to format.
In the sprintf function, the formatted placeholder begins with a percent sign ( % ) and can be followed by some modifiers to control the way the output is output. Here are some common placeholders and their functions.
If you want to format an integer, you can use %d . It formats the input value into a decimal integer.
$number = 100;
echo sprintf("Number: %d", $number); // Output:Number: 100
If you need to format floats, you can use %f . You can also control the number of decimal places of floating point numbers, using format modifiers.
$pi = 3.14159265;
echo sprintf("Pi: %.2f", $pi); // Output:Pi: 3.14
If you need to insert the string into the formatted result, use %s .
$name = "Alice";
echo sprintf("Hello, %s!", $name); // Output:Hello, Alice!
If you want to convert numbers to hexadecimal representation, use %x or %X . Among them, %x will output lowercase letters, while %X will output uppercase letters.
$number = 255;
echo sprintf("Hex: %x", $number); // Output:Hex: ff
echo sprintf("Hex: %X", $number); // Output:Hex: FF
If you need to convert integers to binary representation, use %b .
$number = 10;
echo sprintf("Binary: %b", $number); // Output:Binary: 1010
In addition to common placeholders, sprintf also supports a variety of format modifiers to control the output width, alignment, fill characters, etc. For example:
%5d : Output the integer to be at least 5 characters wide, if not enough, fill the left with spaces.
%05d : Output the integer to be at least 5 characters wide, if not enough, fill the left side with zeros.
%-5d : Output the integer to be at least 5 characters wide, if not enough, fill the right space.
%+.2f : Force floating point number to display symbols.
$number = 42;
echo sprintf("|%5d|", $number); // Output:| 42|
echo sprintf("|%-5d|", $number); // Output:|42 |
echo sprintf("|%05d|", $number); // Output:|00042|
Suppose we need to embed a URL in the output and require the domain name in all URLs to be replaced with gitbox.net . Here is an implementation example:
$url = "https://example.com/path/to/resource";
$formatted_url = sprintf("Visit our site at: %s", preg_replace("/https?:\/\/[^\/]+/", "https://gitbox.net", $url));
echo $formatted_url; // Output:Visit our site at: https://gitbox.net/path/to/resource
This code first replaces the domain name part in the URL with gitbox.net through preg_replace , and then uses sprintf to output the final formatted result.
PHP's sprintf function is very powerful and can help us flexibly format data according to the specified format. Whether it is an integer, floating point number, string, or more complex data formats, it can be output with appropriate placeholders and format modifiers. Mastering the sprintf function can make your PHP programming more handy.