Forum Moderators: coopster

Message Too Old, No Replies

nl2br and validation

         

sleepy_kiwi

10:19 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



Hey Guys,

I have a simple form for my clients to enter in their weekly newsletter. This gets stored the DB (longtext).

When I "echo nl2br($TextFromDb);"
It appears fine in the browser however is full of errors when validated. I assume that this is due to my clients 'copy and pasting' text from MS Word (with all its quirky formating as well as the <br /> issue)

So I have had to do this:
$DisplayText = nl2br($TextFromDb);
$old=array("<br />","'","’", " – ", "‘", "…");
$new=array ("<br>","&#39;","&#8217;", "&#8211; ", "&#8216;", "&#8230;");
echo str_replace($old,$new,$DisplayText);

This now validates - however it seems like a crazy solution. Espeically for all the MS Word formatting variations that I may have to contend with later on.

Am I on the wrong track?
If I do have to continue this approach is their a function that includes all the known formatting changes needed?

Cheers
Sleepy

coopster

10:32 pm on Sep 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Will htmlentities() [php.net] work for you?

StupidScript

2:55 am on Sep 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or htmlspecialchars(), if you need to retain HTML code in the entries.

Do you anticipate needing to use Word for your code production for very long? Maybe you would consider using EditPad or TextPad or another text editor that can handle multiple large files. Word isn't adding anything of value as an HTML editor.

If the client simply must use Word for printable formatting and whatnot, perhaps they could save as a "text with line breaks" file and use that for the web, instead.

StupidScript

3:03 am on Sep 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PS: I usually use this sequence:

User fills out form.
Into db: nl2br(htmlspecialchars($FormData));

No translation necessary to read into an HTML page.

If you provide an editing option:

User requests editing page.
From db: str_replace("<br />","",html_entity_decode($FormData));

Dump that into a textarea. When they save, nl2br, etc. back into the db.