CKTIS / PHP
nl2br()
Insert an HTML line-break element before each newline sequence, preserving the original line endings.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
nl2br(string $string, bool $use_xhtml = true): stringInsert an HTML line-break element before each newline sequence, preserving the original line endings. The default uses the XHTML spelling and false selects the HTML spelling. This function leaves other text alone, so escape plain text separately when composing HTML.
Signature
nl2br(string $string, bool $use_xhtml = true): string
Example output
["A<br />\r\nB<br />\nC","A<br>\r\nB<br>\nC"]
Examples
<?php
$text="A\r\nB\nC";
echo json_encode([nl2br($text),nl2br($text,false)],JSON_UNESCAPED_SLASHES);
Result
["A<br />\r\nB<br />\nC","A<br>\r\nB<br>\nC"]