Wrapping the contents submitted from a textarea in PHP

Posted by Rey Dacoco in

By default, the texts submitted from a textarea in PHP are displayed in an unbroken single line. For instance, if you have entered the following lorem ipsum text:

It will be displayed as:

This simply tells us that PHP ignores the breaklines posted from a textarea control. This glitch cannot be solved by simply using the WRAP attribute of the textarea tag. Fortunately, PHP provide us with several string functions that can use to solve this problem. One of these functions is the wordwrap function which wraps text depending on the number of character specified by the user. The wordwrap function has the following syntax:

wordwrap(“text to wrap”, columnwidth ,breakline);
For instance:

wordwrap(“Hello world”,5,"<br/>")
For a sample usage of wordwrap in solving the textarea wrapping problem, follow these steps:

1. Start your text editor (Notepad, Notepad++, or Programmers Notepad).
2. Enter the following:


<!--if the submit button is not yet clicked-->

<!--display our form-->
<FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" Method="POST" >
  Enter your message:<br>
<TEXTAREA  WRAp="hard" ROWS="8" COLS="30" NAME="usermsg"></TEXTAREA><br>
<INPUT TYPE="Submit" VALUE="Submit" Name="Submitted"></INPUT> <INPUT TYPE="Reset" VALUE="Clear">



<!--display the text from the textarea-->
echo "$msg";

3. Save it as sample.php in the htdocs folder.
4. Launch your web browser.
5. Type the following in the address bar:
6. You should now see the following:

7. Try entering a long text in the textarea to see the wordwrap effect. For more information on using the wordwrap string function visit http://www.php.net. That's all!


Post a Comment


My photo
I am just a simple man with a simple mind. And this is my simple blog. New Posts will be available soon. Enjoy reading and never stop learning:)

Total Pageviews

Link List


Got questions? You can reach me here: