【问题标题】:Remove TinyMCE formatting from database in PHP从 PHP 中的数据库中删除 TinyMCE 格式
【发布时间】:2013-01-31 19:04:10
【问题描述】:

我在 google 上搜索过,其中大部分是关于阻止 TinyMCE 粘贴 JavaScript 视角下的 Word 样式。

我的问题是数据已经在数据库中 (MySQL) 以及用户通过 TinyMCE 粘贴 MS Words 文本的所有格式。

有没有办法使用 PHP 去除所有格式并只保留文本?

一些示例文本:

&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>

注意:在数据库中存储的 HTML 标记为 html 实体(例如,字符 &amp;lt; 存储为 &amp;lt;

【问题讨论】:

  • 你试过了吗:strip_tags(htmlspecialchars_decode( $text ));
  • @John 哦,谢谢,这解决了我的问题。我之前使用了没有htmlspecialchars_decodestrip_tags。难怪它不起作用:)

标签: php string tinymce


【解决方案1】:

您要做的是将 HTML 转换为纯文本。因为这基本上就是您将从 TinyMCE 中获得的内容以及您想要插入到数据库中的内容。

如前所述,简单的方法是在 htmlspecialchars_decode 函数上使用 strip_tags。但是,它将删除一些可能重要的信息,例如 HR 元素。您更愿意使用html2text,这是一个非常容易使用的类,并带有一些使转换更加准确的附加功能。

希望对你有帮助!

【讨论】:

  • 那是你的产品吗?如果是这样,你应该提到它。而且它看起来像一个商业产品。
  • 不,不是。我刚刚偶然发现它并使用了几次。我将链接到一个替代方案,而不是免费用于商业用途(只是发现以前的库中没有任何明确的下载链接)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多