【问题标题】:How to set content with line break and html tags in tinyEditor如何在 tinyEditor 中使用换行符和 html 标签设置内容
【发布时间】:2022-01-16 22:33:54
【问题描述】:

我正在将 tinymce 编辑器 的内容保存在 MySQL 表中,并且想将我从数据库中检索到的相同内容粘贴回编辑器中。
我使用 htmlentity() 函数对输入进行编码,将其保存到数据库中,然后在显示之前使用 html_entity_decode() 对内容进行解码。
<?php echo html_entity_decode($content->post); ?> 将输出:

<p>adf adf adfadf aadf <img src="images/k0RpgvZ.png" alt="image" width="27" height="18" /></p>

我面临两个问题:

  1. 如何将此内容显示为 html,而不仅仅是文本?
  2. 我还想用从数据库中检索到的这个值来设置 tinyEditor 的内容。 这段代码 sn-p 可以做到(取自tiny blog)。
tinymce.init({
    selector: '#myTextarea',
    setup: function (editor) {
      editor.on('init', function (e) {
        editor.setContent('<?php echo $content->post; ?>');
      });
    }
  });

但是,它仅适用于 $content-&gt;post 包含单个单词(无空格、无换行符、无特殊字符)。
只要有换行符或空格,...,我就会得到错误:

Uncaught SyntaxError: '' string literal contains an unescaped line break

如何处理这些问题?

【问题讨论】:

  • 使用 `insted of '。试试这个:editor.setContent(&lt;?php echo $content-&gt;post; ?&gt;);

标签: php html mysql tinymce


【解决方案1】:

试试这个:

editor.setContent(`<?php echo $content->post; ?>`);

【讨论】:

  • 谢谢@KHIMAJI,数据现在已经在编辑器中设置好了。但是有纯文本,像这样:&lt;p&gt;adf adf adfadf aadf &lt;img src="images/k0RpgvZ.png" alt="image" width="27" height="18" /&gt;&lt;/p&gt;
  • 能否请您删除“html_entity_decode”功能并检查?
  • 不使用“htmlentities”和“html_entity_decode”函数检查。
  • 我在将其保存到数据库之前使用htmlentities()。所以它将一些字符转换为 HTML 实体。我想在将其保存到数据库之前使用htmlentities() 以解决安全问题。
  • 然后您需要检查“html_entity_decode”是否正在执行并替换正确的变量,它会显示在“html_entity_decode”之后。根据您的解释,内容显示不使用“html_entity_decode”功能。
猜你喜欢
  • 1970-01-01
  • 2015-11-24
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-08
  • 1970-01-01
  • 2013-03-17
相关资源
最近更新 更多