Support PHP Version: PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1, PHP 8.2, PHP 8.3 With Latest All Version Support.
Hello Friends Today, through this tutorial, I will tell you How to Use `wordwrap()` function using PHP, PHP 8, PHP 8.1, PHP 8.2 With Example. In PHP, the `wordwrap()` function is used to wrap a string to a specific line length, breaking it at word boundaries. This function is particularly useful when you want to format text to fit within a certain width, commonly seen in applications dealing with text display or formatting.
Here’s how the `wordwrap()` function works in PHP 8.1 and 8.2 along with an example:
Syntax:
<?php wordwrap(string $string, int $width, string $break = "\n", bool $cut = false): string|false ?>
1. `$string`: The input string to be wrapped.
2. `$width`: The maximum length of line after wrapping.
3. `$break`: Optional. The character used to break the line. Default is newline character `”\n”`.
4. `$cut`: Optional. If set to `true`, words longer than `$width` will be broken. Default is `false`.
Example:
<?php $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; // Wrap the string to a maximum width of 30 characters $wrappedString = wordwrap($string, 30); echo $wrappedString; ?>
Output:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
In this example, the input string is wrapped to a maximum width of 30 characters, breaking it at word boundaries. The resulting string is then echoed, displaying the wrapped text.
You can also use additional parameters like `$break` and `$cut` to customize the behavior of the `wordwrap()` function according to your requirements.