Skip to content
PHP Logo

What is the correct syntax for using Wordwrap with a callback function in PHP?

When using the `wordwrap` function in PHP with a callback function, the correct syntax involves passing the callback function as the fourth argument. The callback function should accept a single parameter representing the line of text to be wrapped and return the wrapped line. This allows for custom logic to be applied to each line of text before it is wrapped.

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, "<br>", true, function($line) {
    return "<span style='color:red'>" . $line . "</span>";
});

echo $wrapped_text;