In PHP, wordwrap() is a very useful function that automatically wraps a string to a specified width. This function is especially useful when handling long strings to ensure they do not exceed the expected column width in the output. In this article, we will explain how to use the wordwrap() function with some examples and show how to flexibly set the number of characters per line in real-world applications.
The basic syntax of the wordwrap() function is as follows:
wordwrap($string, $width, $break, $cut);
$string: The input string to be processed.
$width: The maximum number of characters per line. If the number of characters in the line exceeds this width, wordwrap() will insert a line break at this position.
$break: The symbol used for line breaks. The default is the newline character \n, but you can set it to any string (such as
).
$cut: Whether to forcibly break words at the $width limit. If set to TRUE, words will be broken even if they are not completely finished at the width limit. If set to FALSE (the default), wordwrap() will break lines at word boundaries.
Let's demonstrate how to use wordwrap() with a simple example:
<?php
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
$wrapped_text = wordwrap($text, 30, "\n");
echo $wrapped_text;
?>
Output:
Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Sed
do eiusmod tempor incididunt ut
labore et dolore magna aliqua.
In this example, the wordwrap() function limits each line to 30 characters and inserts a newline character \n at each line break.
In actual development, you might want to use the
tag for line breaks in HTML output instead of a standard newline character. To achieve this, you can set the $break parameter to
:
<?php
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
$wrapped_text = wordwrap($text, 20, "<br>");
echo $wrapped_text;
?>
Output:
Lorem ipsum dolor<br>sit amet,<br>consectetur<br>adipiscing elit.
In this case, the output can be correctly displayed with line breaks in the web page.
If we want to force words to be broken at the line length limit, we can use the $cut parameter. By default, wordwrap() avoids breaking words in the middle, but when $cut is set to TRUE, it will break words directly at the width limit:
<?php
$text = "Lorem ipsum dolor sit amet";
$wrapped_text = wordwrap($text, 10, "-", true);
echo $wrapped_text;
?>
Output:
Lorem-ipsum-
dolor-sit-
amet
As shown, the wordwrap() function forces word breaks at the width limit of each line.
If there is a URL in the output, we usually want to ensure that the URL is not broken. To achieve this, we can replace the URL with a placeholder before processing the string, then use wordwrap() to wrap the text, and finally replace the placeholder back with the original URL. Here's an example:
<?php
$text = "Visit my website at http://gitbox.net for more information.";
$text_with_placeholder = preg_replace('/http[s]?:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/', 'URL_PLACEHOLDER', $text);
$wrapped_text = wordwrap($text_with_placeholder, 20, "\n");
$final_text = str_replace('URL_PLACEHOLDER', 'http://gitbox.net', $wrapped_text);
echo $final_text;
?>
Output:
Visit my website at
http://gitbox.net
for more information.
In this example, we use a regular expression to find the URL in the text and replace it with a placeholder URL_PLACEHOLDER. After using wordwrap() to wrap the text, we replace the placeholder back with the original URL, ensuring the URL is not broken.
The wordwrap() function is a very practical tool that helps developers automatically wrap long strings to a specified width. In real-world applications, we can customize the line break character and set rules for forced line breaks. If dealing with special characters like URLs, using regular expressions to avoid breaking them is also a common practice.
By using wordwrap() effectively, we can better control the output format, improving the readability of our programs and enhancing the user experience.