On my website I have a news system, it fetches it's content from a my sql databse which the users submit to, well thats all working great When it comes to displaying the information I want it to retain all the formatting such as line breaks and things like that. So I used pre which worked great, the only problem is when a line of text is inserted which is longer than the box it's meant to be in it keeps going, when it was in a <P> it dropped onto the next line. They have the exact same setup in css. So what I want is the formatting of <pre> to allow line breaks and the fitting into the space it's meant to be of <p> I know this may be very simple but I havn't thought of a way. Thankyou in advance
As simon said, nl2br is what you want as browsers ignore formatting (HTML tags are presentation). Here's my 'fixed' version of nl2br which produces proper paragraphs rather than just thousands of <br /> tags. PHP: function fixed_nl2br($str) { $retval = '<p>'; $lines = explode("\n", $str); $c = count($lines); for ($i = 0; $i < $c; $i++) { if ('' == trim($lines[$i]) && 0 != $i) // empty lines are the start and end of paragraphs { $lines[$i] = '</p><p>'; } else if(array_key_exists(($i+1), $lines) && ('' != $lines[$i + 1] && ($i + 1) < $c)) // if the next line isnt empty and we're not counting ahead of the amount of lines we have, we can add a break { $lines[$i] .= '<br />'; } $retval .= $lines[$i]; } $retval .= '</p>'; return $retval; }