xoops forums

macgeek

Just popping in
Posted on: 2004/9/13 11:47
macgeek
macgeek (Show more)
Just popping in
Posts: 5
Since: 2004/9/6 1
#1

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.

Enjoy.

function &nl2Br($text)
{
    
/*
    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);
    
    return 
$text;     
}

macgeek

Just popping in
Posted on: 2004/9/13 14:16
macgeek
macgeek (Show more)
Just popping in
Posts: 5
Since: 2004/9/6 1
#2

Re: A fixed nl2Br -- no more extra white space

Upon further investigation, this horks PRE tags. Still working :)

macgeek

Just popping in
Posted on: 2004/9/13 14:47
macgeek
macgeek (Show more)
Just popping in
Posts: 5
Since: 2004/9/6 1
#3

Re: A fixed nl2Br -- no more extra white space

Feh. Can't get around PRE tags, but adding this above the default line works well. It eliminates line breaks around HTML tags.

//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);

Stewdio

Community Support Member
Posted on: 2004/9/13 14:55
Stewdio
Stewdio (Show more)
Community Support Member
Posts: 1560
Since: 2003/5/7 1
#4

Re: A fixed nl2Br -- no more extra white space

Thanks for the updates and investigation. Perhaps you could also submit your changes to the source forge. I'm assuming of course that this is in the text sanitiser, just so that others know where to look when and if they decide to add your changes.

moss

Just popping in
Posted on: 12/10 14:02
moss
moss (Show more)
Just popping in
Posts: 38
Since: 2009/10/28
#5

Re: A fixed nl2Br -- no more extra white space

Hello,

I have Xoops 2.5.9 and I have this issue with table... I tried to play with module.textsanitizer.php and function nl2Br($text) but it doesn't work.

Can I have some help

Thanks