Formatting Strings
In PHP, you can format strings in various ways to make them more readable and structured. Below are some common methods and functions used for string formatting:
printf and sprintf
These functions are used to format strings using placeholders.
printf - Output a formatted string
<?php
$name = "John";
$age = 25;
// Using printf
printf("My name is %s and I am %d years old.", $name, $age);
// Outputs: My name is John and I am 25 years old.
?>
sprintf - Return a formatted string
<?php
$name = "John";
$age = 25;
// Using sprintf
$formattedString = sprintf("My name is %s and I am %d years old.", $name, $age);
echo $formattedString;
// Outputs: My name is John and I am 25 years old.
?>
number_format - Format Numbers
This function formats a number with grouped thousands.
<?php
$number = 1234567.89;
$formattedNumber = number_format($number, 2, '.', ',');
echo $formattedNumber; // Outputs: 1,234,567.89
?>
str_pad - Pad a String to a Certain Length
This function pads a string to a certain length with another string.
<?php
$input = "Alien";
$padded = str_pad($input, 10, "*", STR_PAD_BOTH);
echo $padded; // Outputs: ***Alien***
?>
str_repeat - Repeat a String
This function repeats a string a specified number of times.
<?php
$input = "Hello";
$repeated = str_repeat($input, 3);
echo $repeated; // Outputs: HelloHelloHello
?>
strtoupper and strtolower - Change Case
These functions convert strings to uppercase or lowercase.
<?php
$input = "Hello, World!";
echo strtoupper($input); // Outputs: HELLO, WORLD!
echo strtolower($input); // Outputs: hello, world!
?>
ucfirst and lcfirst - Capitalize or Lowercase the First Character
These functions modify the first character of a string.
<?php
$input = "hello, World!";
echo ucfirst($input); // Outputs: Hello, World!
echo lcfirst("HELLO, WORLD!"); // Outputs: hELLO, WORLD!
?>
ucwords - Capitalize the First Character of Each Word
This function capitalizes the first character of each word in a string.
<?php
$input = "hello, world!";
echo ucwords($input); // Outputs: Hello, World!
?>
trim, ltrim, rtrim - Remove Whitespace
These functions remove whitespace from the beginning and/or end of a string.
<?php
$input = " Hello, World! ";
echo trim($input); // Outputs: Hello, World!
echo ltrim($input); // Outputs: Hello, World!
echo rtrim($input); // Outputs: Hello, World!
?>
Example with HTML Formatting
You can also format strings to generate HTML content dynamically.
<?php
$name = "John";
$age = 25;
$html = sprintf('<div class="user"><h2>Name: %s</h2><p>Age: %d</p></div>', $name, $age);
echo $html;
// Outputs:
// <div class="user"><h2>Name: John</h2><p>Age: 25</p></div>
?>
Multiline String Formatting with heredoc
Using heredoc syntax for more complex formatting of multiline strings.
<?php
$name = "John";
$age = 25;
$text = <<<EOD
Name: $name
Age: $age
This is a formatted
multiline string.
EOD;
echo nl2br($text);
// Outputs:
// Name: John<br />
// Age: 25<br />
// This is a formatted<br />
// multiline string.
?>
These examples cover a variety of ways to format strings in PHP, helping you create more readable and structured output.