Good morning dudes,

I have a little issue, I have a rich text editor but on copy and pasting it retains a lot of the gunk, unwanted fonts classes. It is even worse when copy and pasting from microsoft word. Here are some of the things I want:

  • To remove all unwanted formatting except bold tags, uls, lis, hyperlinks and p tags
  • On paste either, control +v or right click paste, remove formatting (cross platform solution that works on all browsers
  • Is it best to use javascript and PHP for validation? So should I do a double pass and use a PHP function to clean the input?
  • When I save to the database I have limited the content field to 500 chars, but if the user copy and pastes too much text it will drop a closing html tag and thus mess up my form.
4 Years
Discussion Span
Last Post by diafol

Why don't you check how an editor like TinyMCE does it?

Removing formatting can be a pain, it could be HTML, XML or RTF.


I ended up using


Which looks very good.

To detect copy and paste I am using:


And finally, I'm using PHP dom document to strip id and classes.


I just need a way to hook this all in to my rich text fields so I don't repeat the code.

Thanks for the help.


I'm assuming that this is a javascript issue? You wouldn't use php to clean pasted input before you passed it "from the editor to the server" would you?

Anyway, sanitizing tags is impt in php to ensure no script tags etc are included. Whitelisting tags may be a way of doing it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.