某些字符串中包含html标签,不编码,页面输出就乱了。
htmlentities is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.
除了不加参数直接调用htmlentities()以外,这2个function是等价的。
不加参数直接调用htmlentities()时,会把输入的字符串全部当作html标签去编码,所以会产生乱码。
htmlspecialchars只处理这几种字符:
ENT_NOQUOTES
is not set.ENT_QUOTES
is set.都支持同样的编码参数:
ENT_COMPAT |
Will convert double-quotes and leave single-quotes alone. |
ENT_QUOTES |
Will convert both double and single quotes. |
ENT_NOQUOTES |
Will leave both double and single quotes unconverted. |
ENT_IGNORE |
Silently discard invalid code unit sequences instead of returning an empty string. Using this flag is discouraged as it » may have security implications. |
ENT_SUBSTITUTE |
Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty string. |
ENT_DISALLOWED |
Replace invalid code points for the given document type with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of leaving them as is. This may be useful, for instance, to ensure the well-formedness of XML documents with embedded external content. |
ENT_HTML401 |
Handle code as HTML 4.01. |
ENT_XML1 |
Handle code as XML 1. |
ENT_XHTML |
Handle code as XHTML. |
ENT_HTML5 |
Handle code as HTML 5. |
点击打开链接http://php.net/manual/zh/function.htmlspecialchars.php
js中只有 encodeURI(),encodeURIComponent(),escape()
前两个只对uri编码,后一个编码范围太大
参考: 点击打开链接http://www.w3school.com.cn/jsref/jsref_encodeURIComponent.asp
点击打开链接http://www.w3school.com.cn/jsref/jsref_encodeURIComponent.asp
参考:http://www.cnblogs.com/leaven/archive/2012/07/12/2588746.html点击打开链接
function html_encode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/&/g, ">"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/ /g, " "); s = s.replace(/\'/g, "'"); s = s.replace(/\"/g, """); s = s.replace(/\n/g, "<br>"); return s; }