Kindeditor 修改内容时如何不让 &lt 被自动转义

发布于 2021-11-30 04:28:04 字数 923 浏览 923 评论 5

本人新手,用PHP&MySQL写了个文章发布系统,富文本编辑器使用的是Kindeditor。

症状说明:

我想通过编辑器发些代码,添加文章时可以正常发代码,包括 < 也被正常转义为 &lt ,保存到数据库中也是 &lt。

但如果修改文章,从数据库中读取到的 &lt 会被 KindEditor 自动转义为 < ,导致内容无法正确显示。

以下是 Kindeditor 的调用代码

			KE.show({
				id : 'content',
				urlType: 'absolute',
				imageUploadJson : '../../upload_img.php',	//相对于kindeditorpluginsimageimage.html
				fileManagerJson : '../../select_img.php',	//相对于kindeditorpluginsfile_managerfile_manager.html
				allowFileManager : true,
			});

数据是直接通过 $_POST['content'] 提交到 MySQL 中的,没有任何转义。

翻过 oschina 的帖子,也有人提出类似的问题,红署哥哥的回答是把 & 转义为 &amp; ,于是我就使用PHP的 str_replace()函数把&转义成&amp;。

这样 &lt 就变成了 &amp;lt ,这倒是可以解决 Kindeditor 自动转义 < 的问题,但如果我打一个空格也就是 &nbsp; 也变成了 &amp;nbsp ,无法正常显示空格。

究竟该如何解决特殊字符转义的问题呢?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

恋你朝朝暮暮 2021-12-06 12:51:01

求答案

回眸一笑 2021-12-06 12:50:40

已经用上了,感谢!

function kindhtml($html,$jc='j'){
 if($jc=='j'){
  $html = str_replace(' ', '&nbsp;', $html);
  $html = str_replace('>', '&gt;', $html);
  $html = str_replace('<', '&lt;', $html);
 }
 //仅用在非kindeditor文本编辑器页面使用,因为kindeditor文本编辑器会自动执行下面这类转换
 if($jc=='c'){
  $html = str_replace(' ', '&nbsp;', $html);
  $html = str_replace('>', '&gt;', $html);
  $html = str_replace('<', '&lt;', $html);
 }
}

梦里兽 2021-12-06 07:16:57

试试 @红薯 就这个连续空格的问题是否有处理?

红 薯

红   薯 (3个空格)

红    薯(4个空格)

红     薯(5个空格)

飘然心甜 2021-12-05 07:52:57

把 & 转换成 & 再保存入数据库,在前台输出时需要重新 str_replace('&', '&', $content);

但在 kindeditor 编辑器中不需要,好像它会自己把 & 转换成 & ,我修改文章时看 kindeditor 中的源码 & 都变成了&

别的编辑器也是这样的吗? kindeditor 为什么这样转换呢,感觉麻烦了许多,是不是有什么好处啊?

 

倚栏听风 2021-12-02 23:03:53

oschina 的转换方法如下,确保无误(  转成 &nbsp 还是空格的)

/**
 * 格式化HTML文本
 * @param content
 * @return
 */
public static String rhtml(String content) {
	if(StringUtils.isBlank(content))
		return content;
	String html = content;
	html = StringUtils.replace(html, "&", "&");
	html = StringUtils.replace(html, "<", "<");
	html = StringUtils.replace(html, ">", ">");
	return html;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文