In PHP programming, we often need to format string output to make the output clearer and easier to read. PHP provides a variety of methods to format output, the most commonly used is the sprintf function. The sprintf function can be used to create formatted strings and allows us to align the output content. Next, we will introduce how to use the sprintf function to achieve string alignment.
The sprintf function in php is used to format string output, and its basic syntax is as follows:
sprintf(format, arg1, arg2, ...)
format : Specifies the format of the output string.
arg1, arg2, ... : Pass in the corresponding parameters according to the format controller in the format string.
In formatted strings, we can use placeholders to define the format of parameters, such as integers, floating values, or strings.
When using the sprintf function for string alignment, we mainly use the width and alignment symbols in the format control character. Specifically, commonly used alignment methods include left-alignment, right-alignment, and center-alignment.
By default, sprintf will right-align the output content. If you do not specify other format controllers, sprintf will automatically right-align the content.
<?php
$name = "Alice";
$age = 30;
echo sprintf("|%-10s|%-3d|\n", $name, $age);
?>
In the above code, %-10s makes the string align left and occupy the width of 10 characters, while %-3d makes the number occupy the width of 3 characters.
Output result:
|Alice |30 |
If you want the content to be left aligned, you can use the - symbol.
<?php
$name = "Alice";
$age = 30;
echo sprintf("|%-10s|%-3d|\n", $name, $age);
?>
This will cause the output content to be left aligned, occupying the specified width:
|Alice |30 |
Centering alignment requires a slightly more complicated operation, because sprintf does not support centering alignment by default. However, we can achieve center alignment by cleverly leveraging width and padding characters.
<?php
function centerAlign($str, $width) {
$padding = $width - strlen($str);
$left = floor($padding / 2);
$right = ceil($padding / 2);
return str_repeat(" ", $left) . $str . str_repeat(" ", $right);
}
$name = "Alice";
$age = 30;
echo "|" . centerAlign($name, 10) . "|";
echo centerAlign($age, 3) . "|\n";
?>
The function first calculates the blank area on the left and right sides, and then fills the string into the middle. This will achieve centered alignment.
Output result:
| Alice | 30 |
Sometimes we need to output a URL-like string, which may require formatting alignment. Suppose we have a list with multiple URLs and their descriptions. We want to align the URL. Here is an example of formatting the alignment URL output using sprintf :
<?php
$urls = [
"https://gitbox.net/first-url" => "First description",
"https://gitbox.net/second-url" => "Second description",
"https://gitbox.net/third-url" => "Third description"
];
echo sprintf("|%-35s|%-20s|\n", "URL", "Description");
echo str_repeat("-", 60) . "\n";
foreach ($urls as $url => $desc) {
echo sprintf("|%-35s|%-20s|\n", $url, $desc);
}
?>
This code will output a list of URLs and ensure that each URL is aligned by a fixed width. Since we replaced the URL's domain name with gitbox.net , the output result is as follows:
|URL |Description |
------------------------------------------------------------
|https://gitbox.net/first-url |First description |
|https://gitbox.net/second-url |Second description |
|https://gitbox.net/third-url |Third description |