SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Inserting HTML Line breaks using PHP

To add line breaks in html we have to use <br> tags but to add this line break to any text entry from a form or from a text area we have to use the function nl2br() in PHP. This function nl2br() searches for all the line breaks or carriage returns within a string and replace them with line break tag or <br> tag. This way we can automatically add line breaks for a web page inside a string. Here is the syntax of nl2br() function.

$string=nl2br($string);

The most practical use of this is when users enter their data in a text area and then the details are displayed back to the visitors. Here if the visitor has used line breaks inside the text area then while displaying the details we have to take care of line breaks. The main problem while displaying will be stretching of lines horizontally. Here the single function nl2br() will take care of this.

If you are using a text area where users are entering any message or data with line breaks then we have to retain the same format and replace all the carriage returns by
tags. This can be done at the time of inserting the record to database table so while displaying we need not take care of that and the message will be displayed the way the user has entered. But if you are giving the option of editing ( at that time or in future ) to the user then the same text you have to use br2nl() function and display them inside a text area. So the best way is to store the content of the user without using nl2br() function and at the point of displaying in a browser ( not inside a text box ) we can use nl2br() to ensure the line breaks. So store the content as it is without using nl2br() and only use it while showing in a browser.

Here is a demo of the use of this function. You can see how the data of the text area gets displayed with and without the line breaks. ( The only difference between the two is before displaying the second case nl2br() function is used. )

Discuss this tutorial at forum


PHP String Functions
All String Functions in PHP
str_replace: How to replace a part of a string with another string
str_ireplace: Case in-sensitive search and replace using array of strings
strlen: How to find length of a string in PHP?
trim: Removing empty space from both sides of a string
strrev: Reversing a string by using strrev function in PHP
Adding two or more strings in PHP
stristr: Searching for a presence of a string inside another string
nl2br: Adding Line breaks inside a string in place of carriage returns
split: Breaking a string to form array using delimiters
substr: Collecting part of a string
substr_count: Counting occurrence of sub string in main string
str_repeat: Repeating a string number of times
strtolower(): function to change alphabets to Lower case
strtoupper(): function to change characters to upper case letters
function to collect part of a string with two landmarks
Random string generator with number and alphabets for password
strcasecmp(): Case insensitive string comparison
strcmp(): Case sensitive string comparison
str_pad(): String pad with specified length
md5 hash of a string: encryption of a string
Separating domain and userid part from an email address using split
htmlspecialchars: Printing html special chars to the page
strip_tags: Removing html tags within a string
ucwords: Removing html tags within a string



 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
String Functions
htmlspecialchars
md5 hash
nl2br
Random string
strip_tags
str_replace
str_ireplace
strlen
strrev
stristr
split
substr
substr_count
str_repeat
strtolower
strcasecmp
strcmp
str_pad
trim
ucwords
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.