Member Avatar for iamthwee

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 for iamthwee

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 for iamthwee

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

Member Avatar for iamthwee

I ended up using

http://patisserie.keensoftware.com/en/pages/remove-word-formatting-from-rich-text-editor-with-javascript

Which looks very good.

To detect copy and paste I am using:

http://www.mkyong.com/jquery/how-to-detect-copy-paste-and-cut-behavior-with-jquery/

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

http://stackoverflow.com/questions/3026096/remove-all-attributes-from-an-html-tag

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 for diafol

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, networking, learning, and sharing knowledge.