Posted on: 2004/9/13 11:47
A fixed nl2Br -- no more extra white space
Coming from other CMSes I've found the way XOOPS handles text to be extremely annoying. Luckily, there were a significant amount of other things that made me use it for my upcoming site and I am, overall, quite happy. But I had
to fix that damned "extra white space" problem when posting.
After hunting for a while I found it was the nl2Br function that was very, very simply-implemented. I made a smart version. This version has some simple rules:
1. If there is an HTML tag at either end, remove all white space so it doesn't get parsed. This fixes the "too much white space above the tables" problem.
2. If there are a bunch of new lines between things, put one P tag in its place.
3. It there is one new line in-between two lines, place a BR.
If there are two or more NLs between things, add a <p />
If there is one NL then make it a <br />.
If the last non-white-space char before the line break was
the end of a tag, don't add anything.
//Remove white space between HTML tags and a preceding
//or following new line. (And the new line itself.)
$text = preg_replace("/(<.*?>)s*[rn]s*/","$1", $text);
$text = preg_replace("/s*[rn]s*(<.*?>)/","$1", $text);
//Single returns become single breaks.
//This is before the <p /> handler because it would match these as well.
$text = preg_replace("/([^rn])rn([^rn])/","$1<br />$2", $text);
//Multiple returns become one paragraph break.
$text = preg_replace("/[rn]+/","<p />", $text);
//Remaining chars become breaks.
$text = preg_replace("/n/","<br />", $text);
$text = preg_replace("/r/","<br />", $text);