Member Avatar

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.

Recommended Answers

All 7 Replies

I have a rich text editor

Which one?

Member Avatar

My own, I can send you the files if needed?

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.

Member Avatar

Not looked at that editor, the source code could be a minefield. Any other ideas.

Member Avatar

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.

Member Avatar

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.