3
votes

I have googled about it and most of them are about stopping TinyMCE from pasting Word styles which are in JavaScript perspective.

My problem is the data is already on the database (MySQL) along with all formatting made by users pasting MS Words texts via TinyMCE.

Is there any way to strip all the formatting and keep only texts by using PHP?

Some sample text:

&lt;!--  /* Font Definitions */  @font-face     {font-family:"Cambria Math";    panose-1:2 4 5 3 5 4 6 3 2 4;   mso-font-charset:1;     mso-generic-font-family:roman;  mso-font-format:other;  mso-font-pitch:variable;    mso-font-signature:0 0 0 0 0 0;} @font-face     {font-family:Calibri;   panose-1:2 15 5 2 2 2 4 3 2 4;  mso-font-charset:0;     mso-generic-font-family:swiss;  mso-font-pitch:variable;    mso-font-signature:-1610611985 1073750139 0 0 159 0;}  /* Style Definitions */  p.MsoNormal, li.MsoNormal, div.MsoNormal    {mso-style-unhide:no;   mso-style-qformat:yes;  mso-style-parent:"";    margin-top:0in;     margin-right:0in;   margin-bottom:10.0pt;   margin-left:0in;    line-height:115%;   mso-pagination:widow-orphan;    font-size:11.0pt;   font-family:"Calibri","sans-serif";     mso-ascii-font-family:Calibri;  mso-ascii-theme-font:minor-latin;   mso-fareast-font-family:Calibri;    mso-fareast-theme-font:minor-latin;     mso-hansi-font-family:Calibri;  mso-hansi-theme-font:minor-latin;   mso-bidi-font-family:"Times New Roman";     mso-bidi-theme-font:minor-bidi;     mso-fareast-language:EN-US;} .MsoChpDefault     {mso-style-type:export-only;    mso-default-props:yes;  mso-ascii-font-family:Calibri;  mso-ascii-theme-font:minor-latin;   mso-fareast-font-family:Calibri;    mso-fareast-theme-font:minor-latin;     mso-hansi-font-family:Calibri;  mso-hansi-theme-font:minor-latin;   mso-bidi-font-family:"Times New Roman";     mso-bidi-theme-font:minor-bidi;     mso-fareast-language:EN-US;} .MsoPapDefault     {mso-style-type:export-only;    margin-bottom:10.0pt;   line-height:115%;} @page Section1   {size:8.5in 11.0in;     margin:1.0in 1.0in 1.0in 1.0in;     mso-header-margin:.5in;     mso-footer-margin:.5in;     mso-paper-source:0;} div.Section1   {page:Section1;} --&gt;   Blah blah blah blah blah &nbsp;    </p>

Note: The HTML tags stored in database as html entities (for example, character < is stored as &lt;)

1
Have you tried: strip_tags(htmlspecialchars_decode( $text )); ?Green Black
@John oh thanks, that solved my problem.. I used the strip_tags without htmlspecialchars_decode before.. no wonder it didn't work :)akhy

1 Answers

1
votes

What you want to do is to convert HTML into a plain text. As that's basically what you'll get out of TinyMCE and what you want to insert into the database.

The simple way to do that is, as already mentioned, using strip_tags on htmlspecialchars_decode function. However it will remove some possibly important information, like for example HR elements. What more likely you'd like to use is html2text which is quite easy class to use with some additional features that make the conversion far more accurate.

Hope it helps!