Jump to content

stripping newline from phpmyadmin edit box


salami1_1

Recommended Posts

Hi,

 

My situation is as following:

I have some text+html data (html data is encoded htmlspecialchars) in my database. Now some of this data contains newlines. If I go into my phpmyadmin and edit the values manually I can see newlines.

 

Now I want to maintain these newlines as I use them on a part on the website.

What I'm trying is to generate a .xls (csv) file but the newlines are screwing with my layout they are create, how obviously, new lines in my excel document which I do not want.

 

My code to solve this atm:

    $en = htmlspecialchars_decode($row['en'], ENT_QUOTES);
    $other = htmlspecialchars_decode($row[$taal], ENT_QUOTES);
            $col1 = str_replace("\r\n","", $en);
            $col2 = str_replace("\r\n","", $other);
            $col1 = str_replace("\n","", $en);
            $col2 = str_replace("\n","", $other);
            $col1 = str_replace("\l","", $en);
            $col2 = str_replace("\l","", $other);
            
            $_SESSION['filedata'] .= $row['blockname']."\t$col1\t$col2\t".nl2br($row['vars'])."\n";

 

as you see I attempt to remove all newlines tags but well when I download the file it just doesn work.. I stil lget newlines where I can also find them in the edit boxes in phpmyadmin.

 

Anybody any idea?

 

Best regards,

After a lot of searching and trying I found the cause and looking at the behaviour I would almost say this is a php bug..

 

The

htmlspecialchars_decode()

function is the reason that the 'invisble' newlines are created even when I filter the \n (and \r\n) away with str_replace() before or after doing

htmlspecialchars_decode()

.

 

The <br /> itself does not cause the newline as I checked for this (sometimes there was a double <br /><br /> and this did NOT cause 2 new lines). Now I have it all working with the following code:

 

            $col1 = str_replace("\r\n","", $row['en']);
            $col2 = str_replace("\r\n","", $row[$taal]);
            $col1 = str_replace("\n","", $col1);
            $col2 = str_replace("\n","", $col2);
            $col1 = str_replace("\t","", $col1);
            $col2 = str_replace("\t","", $col2);
            
            $col1 = str_replace("<","<", $col1);
            $col1 = str_replace(">",">", $col1);
            $col2 = str_replace("<","<", $col2);
            $col2 = str_replace(">",">", $col2);
            
            
            $_SESSION['filedata'] .= $row['blockname']."\t$col1\t$col2\t".nl2br($row['vars'])."\t\n";

 

Only problem now is that I need to convert all the conversions htmlspecialchars() does, back to normal manually using str_replace(). Which is to say at least very annoying...

 

If anybody has some alternative solution for this or can comment on this problem. Gladly.

 

Best regards,

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.