What are the limitations of using wordwrap() in PHP to handle line breaks in strings?

The wordwrap() function in PHP is useful for breaking long lines of text into shorter lines by inserting line breaks. However, it does not take into account HTML tags, which can result in broken HTML markup if used on strings containing HTML. To handle line breaks in strings containing HTML, you can use the nl2br() function to insert HTML line breaks (<br>) after each newline character (\n).

$text = &quot;This is a long string with &lt;b&gt;HTML tags&lt;/b&gt; that needs line breaks.&quot;;
$wrapped_text = wordwrap($text, 40);
$fixed_text = nl2br($wrapped_text);

echo $fixed_text;