Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying whitespace in HTML when pulling from MySQL TEXT column

Tags:

html

php

mysql

I have saved input from a textarea element to a TEXT column in MySQL. I'm using PHP to pull that data out of the database and want to display it in a p element while still showing the whitespace that the user entered (e.g. multiple spaces and newlines). I've tried a pre tag but it doesn't obey the width set in the containing div element. Other than creating a PHP function to convert spaces to &nbsp and new lines to br tags, what are my options? I'd prefer a clean HTML/CSS solution, but any input is welcome! Thanks!

like image 511
Ryan Ische Avatar asked Oct 01 '08 00:10

Ryan Ische


2 Answers

You could just use PHP's nl2br function.

like image 167
Ian Oxley Avatar answered Oct 16 '22 14:10

Ian Oxley


You can cause the text inside the pre to wrap by using the following CSS

pre {
 white-space: pre-wrap;       /* css-3 */
 white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
 white-space: -pre-wrap;      /* Opera 4-6 */
 white-space: -o-pre-wrap;    /* Opera 7 */
 word-wrap: break-word;       /* Internet Explorer 5.5+ */
}

Taken from this site

It's currently defined in CSS3 (which is not yet a finished standard) but most browsers seem to support it as per the comments.

like image 33
Vincent McNabb Avatar answered Oct 16 '22 15:10

Vincent McNabb