PHP & Javascript 如何对字符串中包含html标签进行编码 整理
2014-11-25 11:35
886 查看
为什么要对字符串编码?
某些字符串中包含html标签,不编码,页面输出就乱了。PHP下怎么对字符串编码?
htmlentities
vs htmlspecialchars
htmlentities 与htmlspecialchar 区别:
htmlentities isidentical 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只处理这几种字符:
'&' (ampersand) becomes '&'
'"' (double quote) becomes '"' when
ENT_NOQUOTESis
not set.
"'" (single quote) becomes ''' (or ') only when
ENT_QUOTESis
set.
'<' (less than) becomes '<'
'>' (greater than) becomes '>'
都支持同样的编码参数:
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
javascript 中怎么对字符串编码?
js中只有 encodeURI(),encodeURIComponent(),escape()前两个只对uri编码,后一个编码范围太大
参考: 点击打开链接http://www.w3school.com.cn/jsref/jsref_encodeURIComponent.asp
点击打开链接http://www.w3school.com.cn/jsref/jsref_encodeURIComponent.asp
为什么要对URI编码?
参考:/article/4863818.html点击打开链接js中如何对包含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; }
相关文章推荐
- PHP & Javascript 如何对字符串中包含html标签进行编码 整理
- php对包含html标签的字符串进行截取的函数分享
- php对包含html标签的字符串进行截取的函数分享
- HTML提交表单用户选择的头像如何利用<select>标签提交到PHP并连接数据库进行存读操作
- javascript用escape编码后如何用php进行解码?
- [PHP] PHP & HTML & JavaScript & MySQL 代码如何互相传值
- javascript对字符串进行html编码
- php向前端发送的字符串中存在HTML标签如何正常显示
- php 判断字符串中是否包含html标签
- php 判断字符串中是否包含html标签
- php 如何清空字符串中的HTML标签
- [求教] - 关于ASP中如何对数据表字段中所包含的特殊字符 ' 与 " 进行转义处理?
- struts2返回到JSP页面的html字符串如何在JSP页面上解析成html标签
- 对字符串进行HTML编码和解码的JavaScript函数
- 字符串内特殊字符在html_javascript_xml等内使用时的转换 ,"缺少十六进制数字"错误的处理
- IE Bug 1 - 错误识别出现在javascript脚本中的字符串,导致HTML标签解析错误
- php下清空字符串中的HTML标签的代码
- javascript中存在几种对URL字符串进行编码的方法
- 将DataGrid表格上字符串的HTML编码进行到底!
- javascript中存在几种对URL字符串进行编码的方法