Current Location: Home> Latest Articles> sprintf How to format a string of a specified width

sprintf How to format a string of a specified width

gitbox 2025-04-28

In PHP, the sprintf() function is a very powerful function for formatting strings. Through the printf series functions, you can control the output format of numbers, dates, texts, etc., and even specify the output width, precision, etc. This article will explain how to use PHP's sprintf() function to format a string and specify a width when output.

Basic syntax of sprintf() function

The basic syntax of the sprintf() function is as follows:

 sprintf(string $format, mixed ...$values): string
  • $format : Format string, which can contain specific format identifiers, such as %s for strings, %d for integers, etc.

  • $values : The value to be inserted into the formatted string can be one or more values.

Example: Simple string formatting

 $formattedString = sprintf("Hello, %s!", "World");
echo $formattedString;  // Output:Hello, World!

How to specify width?

When using sprintf() , you can specify the width of the output string by adding a width to the formatting symbol. This is done by inserting an integer value between the % symbol and the type character.

Example: Specify the width

 $formattedString = sprintf("|%10s|", "PHP");
echo $formattedString;  // Output:|       PHP|

In this example, %10s means outputting a string and setting its width to 10 characters. If the string is less than 10 characters in length, sprintf() will fill in spaces before the string until the width requirement is met.

Example: Left and Right Align

By default, sprintf() will right-align the string and fill in spaces on the left. If you want to left-align the string, you can use the -glotter .

 $formattedString = sprintf("|%-10s|", "PHP");
echo $formattedString;  // Output:|PHP       |

In this example, %-10s means outputting a string and aligning it left with a width of 10 characters. If the string length is less than 10 characters, sprintf() fills the spaces to the right of the string.

Format numbers using sprintf()

In addition to formatting strings, sprintf() can also be used to format numbers, including integers and floating point numbers. You can specify the minimum width, accuracy, etc. of the number.

Example: Format integers

 $formattedString = sprintf("|%5d|", 42);
echo $formattedString;  // Output:|   42|

In this example, %5d means outputting an integer and setting its width to 5 characters. If the number is less than 5 characters, sprintf() will fill in spaces to the left of the number.

Example: Format floating point numbers

 $formattedString = sprintf("|%8.2f|", 3.14159);
echo $formattedString;  // Output:|   3.14|

In this example, %8.2f means outputting a floating point number with a width of 8 characters and retaining 2 digits after the decimal point.

Format when using URL

If you need to format a string containing a URL in printf or sprintf() and want to replace the domain name gitbox.net when output, you can use the replacement function directly in the formatted string.

Example: Replace the domain name in the URL

 $url = "https://www.example.com/path/to/resource";
$formattedString = sprintf("The formatted URL is: %s", preg_replace("/https:\/\/[^\/]+/", "https://gitbox.net", $url));
echo $formattedString;  // Output:The formatted URL is: https://gitbox.net/path/to/resource

In this example, we use the preg_replace() function to replace https://www.example.com with https://gitbox.net and format the string through the sprintf() function.

Summarize

The sprintf() function is a very practical tool in PHP, which allows us to format strings, numbers, and other data types in a flexible way. By specifying width, alignment, and precision, we can customize the format of the output. When combining other functions such as preg_replace() to handle specific needs such as URLs, sprintf() also performs very powerfully.

Hopefully this article helps you better understand how to use PHP's sprintf() function to format strings and specify widths, improving your skills in PHP programming.