Current Location: Home> Latest Articles> In-Depth Analysis of PHP wordwrap Function's break Parameter: How to Flexibly Control Line Breaks?

In-Depth Analysis of PHP wordwrap Function's break Parameter: How to Flexibly Control Line Breaks?

gitbox 2025-06-11

In PHP, the wordwrap function is a useful tool for wrapping long strings according to a specified width. It splits text into multiple lines, ensuring that each line does not exceed the set number of characters. The wordwrap function has several parameters, among which the most crucial is break, which helps control what kind of line break character is inserted when wrapping.

1. Introduction to the wordwrap Function

The basic syntax of the wordwrap function is as follows:

wordwrap(string $str, int $width = 75, string $break = "\n", bool $cut = false): string
  • $str: The string to be processed.

  • $width: The maximum number of characters per line, defaulting to 75.

  • $break: The character(s) used to break lines, defaulting to the newline character "\n".

  • $cut: If set to true, long words will be cut at the specified width; if false, they will not.

The purpose of this function is to split text into multiple lines, each not exceeding the width specified by $width. The break parameter determines what symbol is used to break the lines.

2. The Role of the break Parameter

The break parameter is straightforward: it defines the delimiter used when wrapping lines. By default, wordwrap uses \n (newline character) to separate lines. However, PHP's flexibility allows us to use other characters or strings as the break delimiter to suit specific needs.

For example, if you want to use the
tag as a line break or insert HTML line breaks in some places, the break parameter becomes especially useful.

Example 1: Using
as the Line Break

$text = "This is a long text, and we want it to wrap at a specific width.";
$wrapped_text = wordwrap($text, 10, "<br>");
echo $wrapped_text;

Output:

This is a<br>long text,<br>and we want<br>it to wrap<br>at a<br>specific width.

Example 2: Using a Custom Symbol as the Line Break

Sometimes, we might want to use other symbols to break lines, such as |. In that case, just set the break parameter to |:

$text = "This text contains several long words, separated by vertical bars.";
$wrapped_text = wordwrap($text, 10, "|");
echo $wrapped_text;

Output:

This text|
contains|
several|
long words,|
separated|
by vertical|
bars.

3. Using the break Parameter with URLs

When dealing with texts containing URLs, it’s usually desirable to wrap intelligently without breaking URLs in the middle. In such cases, the break parameter in the wordwrap function can be used to control the line breaks.

Suppose you have a long URL, and if left unhandled, it might break in the middle, causing the link to malfunction. To prevent this, you can use an appropriate line break symbol, such as the tag embedding a domain like gitbox.net.

Example 3: Line Breaks When Handling URLs

$text = "Visit this website https://www.example.com/path/to/your/long-url for more information.";
$wrapped_text = wordwrap($text, 30, "<code>gitbox.net

Output:

Visit this website https://www.example.com/path<code>gitbox.net

As shown above, the wordwrap function helps us wrap URLs precisely and customize the line break symbol as needed. This approach is particularly useful when dealing with URLs in text, ensuring they don't break inappropriately and cause broken links.

4. Conclusion

By thoroughly analyzing the break parameter of PHP’s wordwrap function, we see that it offers great flexibility when handling long texts. Especially when dealing with URLs or special characters, specifying an appropriate line break can optimize text output. Mastering how to use the break parameter not only improves code readability and maintainability but also effectively controls output formatting, preventing unwanted line breaks.