Using PHP's wordwrap() Function for String Wrapping

This article explains PHP's wordwrap() function, detailing its syntax, parameters, and providing multiple practical code examples demonstrating how to wrap long strings, customize break characters, handle word cutting, and process multi-line or special-character strings.

php Courses
php Courses
php Courses
Using PHP's wordwrap() Function for String Wrapping

wordwrap() Function Overview

In PHP development, string manipulation is common. The wordwrap() function inserts line breaks so that each line reaches a specified length.

Syntax

wordwrap(string $str, int $width = 75, string $break = "
", bool $cut = false): string

Parameter Explanation

$str

: the input string to be wrapped. $width: maximum line length (default 75). $break: line break string (default "\n"). $cut: whether to cut words; true forces breaking inside words, false breaks at word boundaries.

Application Examples

1. Wrap a long string

<?php
$str = "This is a long string that needs to be wrapped to fit within a certain width.";
$width = 20;
$result = wordwrap($str, $width);
echo $result;
// Output:
// This is a long
// string that needs to
// be wrapped to fit
// within a certain
// width.
?>

2. Specify break character and width

<?php
$str = "This is a long string that needs to be wrapped to fit within a certain width.";
$width = 15;
$break = "<br>";
$result = wordwrap($str, $width, $break);
echo $result;
// Output:
// This is a long<br>string that<br>needs to be<br>wrapped to fit<br>within a certain<br>width.
?>

3. Break inside words

<?php
$str = "This is a long string that needs to be wrapped to fit within a certain width.";
$width = 15;
$break = "<br>";
$cut = true;
$result = wordwrap($str, $width, $break, $cut);
echo $result;
// Output:
// This is a long<br>string that<br>needs to be<br>wrapped to fit<br>within a<br>certain width.
?>

4. Wrap multi-line strings

<?php
$str = "Hello
World";
$width = 10;
$result = wordwrap($str, $width);
echo $result;
// Output:
// Hello
// World
?>

5. Handle strings with special characters

<?php
$str = "This is a long string that needs to be wrapped to fit within a certain width.";
$width = 15;
$break = "<br>";
$result = wordwrap($str, $width, $break);
echo htmlspecialchars($result);
// Output:
// This is a long<br>string that<br>needs to be<br>wrapped to fit<br>within a certain<br>width.
?>

Conclusion

The wordwrap() function is a convenient tool for string wrapping in PHP, allowing developers to control line length, break characters, and word cutting to improve code readability and output formatting.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

StringTutorialwordwrap
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.