我有一些Ajaxing,我有2块源代码。第一个是:如何在JavaScript/PHP中查看2段代码是否相同?
window.lastSavedContents = "<?php echo htmlentities($contents); ?>";
第二个是用户键入到文本编辑器中的任何内容。问题是window.lastSavedContents
是htmlentities
编码,而第二个块不是。
如何检查等效性?
我有一些Ajaxing,我有2块源代码。第一个是:如何在JavaScript/PHP中查看2段代码是否相同?
window.lastSavedContents = "<?php echo htmlentities($contents); ?>";
第二个是用户键入到文本编辑器中的任何内容。问题是window.lastSavedContents
是htmlentities
编码,而第二个块不是。
如何检查等效性?
首先,不使用htmlentities
,但htmlspecialchars
,只有编码<
,>
,&
和"
。这样,你可以使用
var decoded = encoded.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/"/g, '"')
.replace(/>/g, '>');
只需更换这些实体如果你仍然想使用htmlentities
你可以简单地创建一个临时div
:
var div = document.createElement('div');
div.innerHTML = encoded;
var decoded = div.firstChild.nodeValue;
你可以找到围绕实体编码数据的方法,或者在Javascript中解码window.lastSavedContents
。这里有一个功能,这将帮助你做到这一点:http://phpjs.org/functions/html_entity_decode:424