gpt4 book ai didi

php - CKEditor 将以所见即所得模式插入的 HTML 标签呈现为 PHP/MySQL 中的实际输出

转载 作者:行者123 更新时间:2023-11-28 23:26:50 25 4
gpt4 key购买 nike

我正在使用 CKEditor 4.5.3。这篇文章的标题听起来很严重,我遇到了一个严重的问题。我进一步解释 - 当我将 HTML 标签放入 CKEditor 所见即所得模式时,如下所示:

This is a <h1> heading tag.

在源代码模式下,它转换为:

<p>This is a &lt;h1&gt; heading tag.</p>

所以在这个阶段一切似乎都很好。

我正在使用 PHP/MySQL 将所有这些信息保存在数据库中。在我的 MySQL 数据库中,上述信息存储为:

<p>This is a &lt;h1&gt; heading tag.</p>

在我的网站前端,信息显示为:

This is a <h1> heading tag.

所以在这个阶段一切都正常了。

但是当我在我的管理面板中打开这篇文章进行编辑时,整个困惑就从这里开始了。 CKEditor 的所见即所得模式将存储在我的数据库中的信息显示为:

enter image description here

很明显,HTML 标签 <h1>实际上已经转换为标题输出本身,而不是将其显示为标签。因此,如果我更新帖子,前端的浏览器输出最终将转换为:

enter image description here

我不明白问题出在哪里 - 是在 CKEditor 中还是与 PHP/MySQL 相关的东西?

我怎样才能避免这一切的发生?

编辑

应用CKEditor的HTML表单域:

<tr>
<th><label for="txtpostcontent">Content:</label></th>
<td><textarea name="txtpostcontent" id="txtpostcontent" class="txtafield editor"><?php echo $post_content; ?></textarea>
</td>
</tr>

一些PHP代码:

// Here we fetch the content from database and store in a variable
$post_content = $post[0]->post_content;

// Here we print the array which stores the information from database
Array
(
[0] => stdClass Object
(
[post_id] => 9
[post_name] => HTML Headings
[post_url] => html-headings
[post_content] =>

this is a <h1> heading.





[post_date_published] => 2016-02-22 15:54:51
)

)

// On editing, we store the post content in a variable
$post_content = isset($_POST['txtpostcontent']) ? $_POST['txtpostcontent'] : '';

注意:在此过程中没有使用 AJAX。表格以常规方式提交。

最佳答案

您没有提供示例,但我要冒昧地说它是编码。考虑这个 HTML:

<input type="text" value="Some HTML: <h1>">

调用 jQuery('input:first').val()将导致:Some HTML: <h1>

然后是这个 HTML:

<input type="text" value="Some HTML: &lt;h2&gt;">

同样的 JS 会产生这样的结果:Some HTML: <h2> .这不直观,对吧?这是你问题的根源。考虑一下:

<input type="text" value="Some HTML: &amp;lt;h3&amp;gt;">

现在,JS 将产生我们想要的结果:Some HTML: &lt;h3&gt; .

为确保您的 CKEditor 获得正确的 HTML 代码,您必须调用 htmlentities()在将其插入输入的 value="" 之前在 HTML 上.

在您的情况下,您使用的是文本区域,因此它可能会略有不同。但是我找到了一个 blog post对于类似(相同?)的问题得出了与我相同的结论:语法高亮插件搞砸了编码,因此他不得不转换 htmlentities(博文的作者选择仅转换 &lt;&gt; ,不确定这是否重要)。

关于php - CKEditor 将以所见即所得模式插入的 HTML 标签呈现为 PHP/MySQL 中的实际输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38977332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com