今天遇到一个挺有趣的字符编码转义,通过html的textarea表单jquery ajax post内容到php,然后存储到mysql数据库,然后ajax根据post 的 data 回显,如果刷新页面,通过php 输出mysql 存储的编码; 首先引用一下转义字符串的基础知识。 转义字符串 基本知识 转
今天遇到一个挺有趣的字符编码转义,通过html的textarea表单jquery ajax post内容到php,然后存储到mysql数据库,然后ajax根据post 的 data 回显,如果刷新页面,通过php 输出mysql 存储的编码;
首先引用一下转义字符串的基础知识。
转义字符串 基本知识转义字符串(escape sequence),即字符实体(character entity)分成三部分:第一部分是一个&符号,英文叫ampersand;第二部分是实体(entity)名字或者是#加上实体(entity)编号;第三部分是一个分号。
比如,要显示小于号(
用实体(entity)名字的好处是比较好理解,一看lt,大概就猜出是less than的意思,但是其劣势在于并不是所有的浏览器都支持最新的entity名字。而实体(entity)编号,各种浏览器都能处理。
提示:实体名称(entity)是区分大小写的。 同一个符号,可以用“实体名称”和“实体编号”两种方式引用,“实体名称”的优势在于便于记忆,但不能保证所有的浏览器都能顺利识别它,而“实体编号”则没有这种担忧,但它实在不方便记忆。
js 将html字符编码 转换到 特殊字符定义一个函数decodeentities将html字符编码 转换到 特殊字符:
function decodeentities(s){
var str, temp= document.createelement(‘p’);
temp.innerhtml= s;
str= temp.textcontent || temp.innertext;
temp=null;
return str;
}
这个函数,将会把' & 等直接转换为’ (单引号) 和 &
比如 you appreciate the driver's consideration , & 就会变成 you appreciate the driver’s consideration , &
php 将html字符编码 转换到 特殊字符php 默认有函数将 html字符编码 转换到 特殊字符
html_entity_decode (php 4 >= 4.3.0, php 5)
string html_entity_decode ( string $string [, int $flags = ent_compat | ent_html401 [, string $encoding = 'utf-8' ]] )
html_entity_decode — convert all html entities to their applicable characters (转换所有的html字符编码到对应的特殊字符串)
注意这个函数的参数ent_compat 默认会转换双引号,不会转换单引号;
为了能够同时转换单引号和双引号,需要使用 int $flags = ent_quotes
通过以下的这段php处理数据库中的值:
html_entity_decode($node->body, ent_quotes | ent_html401)
这个函数,将会把' & 等直接转换为’ (单引号) 和 &
比如 you appreciate the driver's consideration , & 就会变成 you appreciate the driver’s consideration , &
整个流程操作假设在html文本框中输入这三行内容:
you appreciate the driver's consideration , &
you appreciate the driver's consideration , &
you appreciate the driver’s consideration , &
如下图:
点击“确定”按钮后,执行jquery ajax post 操作 通过php插入到数据库;
(...)
read the rest of html字符编码js和php 转义 (108 words)
© lixiphp for lixiphp, 2013. |permalink |no comment |add todel.icio.us
post tags: html, jquery, js, mysql, php, 字符编码, 转义
feed enhanced by better feed from ozh