PHP nl2br

 The PHP nl2br function will add HTML line break tags before the "line" in the string <br> to allow the browser to display line breaks correctly, instead of linking each line together. Usually used when the input terminal enters the textarea in the text input field of the webpage to enter a text paragraph containing line breaks. After it is stored in the database, the symbol representing the line break such as "\n" will also be stored in the database, but when PHP is from the database When the data is read out and displayed on the web page, it will not wrap automatically. In this case, the "\n" of the string must be converted into HTML newline tags using the nl2br function .


PHP nl2br basic syntax

nl2br( $string )


The string $string in the grammar parentheses is a required item, which is the string to be converted. The newline symbols that nl2br can determine are such as \r\n , \n\r , \n and \r . When these symbols are used, nl2br will convert them into <br>, <br/> or </br> tags and return the conversion result.

PHP nl2br example
<?php
$new_string = "Welcome to Wibibi.\nIt was a very nice day.";
echo nl2br($new_string);
?>
The above output web results
Welcome to Wibibi.
It was a very nice day.
View code
Welcome to Wibibi.<br />
It was a very nice day.
There is a newline tag \n in the example string. This tag browser can't judge and display a newline, so we used nl2br to convert. The output result does have a newline effect. Check the source code and you can see the extra A <br /> HTML line break tag.

Post a Comment

0 Comments