Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyMCE- Get plain text

Tags:

tinymce

In tinyMCE, Is there any way to get the plain text instead of HTML text?

like image 694
Sivasubramaniam Arunachalam Avatar asked Mar 13 '11 07:03

Sivasubramaniam Arunachalam


3 Answers

Try this:

var myText = tinyMCE.activeEditor.selection.getContent({ format: 'text' });
like image 173
luchopintado Avatar answered Oct 06 '22 01:10

luchopintado


var rawtext = tinyMCE.activeEditor.getBody().textContent;
like image 20
Kapil Bodke Avatar answered Oct 06 '22 01:10

Kapil Bodke


I just tried this approach:

editor.getContent()
   .replace(/<[^>]*>/ig, ' ')
   .replace(/<\/[^>]*>/ig, ' ')
   .replace(/&nbsp;|&#160;/gi, ' ')
   .replace(/\s+/ig, ' ')
   .trim();
  • Replaces both opening and closing html tags with space
  • Replaces various known special characters with space (add yours as well)
  • Replaces multiple spaces with a single space

Worked reasonably well, but it is obviously not perfect. I need only an approximation of plain text for purposes of word counting, so I am willing to ignore corner cases such as having part of the word bold or italic (replacement above for <b>a</b><i>x</i> will produce two separate words a b instead of ab).

It is an extension of Regular expression to remove HTML tags from a string

Hope that helps.

like image 44
Dmitry Frenkel Avatar answered Oct 06 '22 00:10

Dmitry Frenkel