views:

103

answers:

2

I have a CRUD maintenance screen with a custom rich text editor control (FCKEditor actually) and the program extracts the formatted text as HTML from the control for saving to the database. However, part of our standards is that leading and trailing whitespace needs to be stripped from the content before saving, so I have to remove extraneous &nbsp; and <br> and such from the beginning and end of the HTML string.

I can opt to either do it on the client side (using Javascript) or on the server side (using Java) Is there an easy way to do this, using regular expressions or something? I'm not sure how complex it needs to be, I need to be able to remove stuff like:

<p><br /> &nbsp;</p>

yet retain it if there's any kind of meaningful text in between. (Above snippet is from actual HTML data saved by the tester)

+2  A: 
/<p>(?:<br\s*\/>|&[#\w]{2,6};|[\s\n\r])*?<\/p>/g

That should match all paragraphs that don't contain any "meaningful text".

It's probably best to do it on the server-side though.

J-P
shouldn't you consider some entities to be meaningful? i.e. © ?
p3t0r
Not just © ... I'd say most entity references could be classed as "meaningful text" but I think the OP can take care of that. If need be, just remove the middle part of the regex ("|")...
J-P
A: 

Minify the HTML using markupmin.js and then you can use JavaScript to remove the single space characters that may be left over at the beginning or end of your text content. You can access markupmin.js from the Pretty Diff tool, which is an HTML page.

http://mailmarkup.org/prettydiff/markupmin.js

http://mailmarkup.org/prettydiff/prettydiff.html