您好,欢迎访问一九零五行业门户网

PHP中我经常容易混淆的三组函数,PHP混淆三组函数_PHP教程

php中我经常容易混淆的三组函数,php混淆三组函数原文:http://www.ido321.com/1252.html
一、htmlentities() 和htmlspecialchars()
       1、htmlentities()
             1.1  功能:把字符转换为 html 实体。字符包括ascii实体和iso 8859-1实体(html实体对照表:http://www.w3school.com.cn/tags/html_ref_entities.html)
             1.2  语法:htmlentities(string,quotestyle,character-set)
             1.3  参数:string是必选参数,是需要转换的字符串。其余可选,quotestyle规定如何编码单引号和双引号:ent_compat – 默认。仅编码双引号;ent_quotes – 编码双引号和单引号;ent_noquotes – 不编码任何引号。character-set是规定转换用的字符集,常用的有utf-8/gb-2312/iso-8859-1(默认)。
             1.4  提示:无法被识别的字符集将被忽略,并由 iso-8859-1 代替。
$str = john & 'adams';echo htmlentities($str);//在浏览器中输出:john & 'adams'//查看源代码:john & 'adams'

2、htmlspecialchars()
2.1 把一些预定义的字符转换为 html 实体。预定义字符都是ascii 实体,即此函数不能转换iso 8859-1实体,这是和htmlrntities()的区别
预定义的字符是:
& (和号) 成为 & ” (双引号) 成为 " ‘ (单引号) 成为 ' (大于) 成为 > 2.2 htmlspecialchars(string,quotestyle,character-set)
2.3 参数htmlentities()
2.4 提示:无法被识别的字符集将被忽略,并由 iso-8859-1 代替。
$str = john & 'adams';echo htmlentities($str);//在浏览器中输出:john & 'adams'//查看源代码:john & 'adams'

二、html_entity_decode()和htmlspecialchars_decode()
html_entity_decode(string,quotestyle,character-set) 函数把 html 实体转换为字符,是htmlentities()的反函数。
htmlspecialchars_decode(string,quotestyle)函数把预定义的 html 实体转换为字符,是htmlspecialchars()的反函数。
$str = john & 'adams';echo html_entity_decode($str);//浏览器输出:john & 'adams'//源代码:john & 'adams'
三、addslashes()和addcslashes()
1、addslashes(string):在指定的预定义字符前添加反斜杠。string是需要检查的字符串。该函数数可用于为存储在数据库中的字符串以及数据库查询语句准备合适的字符串。
预定义字符是:单引号(’)、双引号(”)、反斜扛(\)和null
ps:默认情况下,php 指令 magic_quotes_gpc 为 on,对所有的 get、post 和 cookie 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。
$str = who's john adams?;echo $str . this is not safe in a database query.
;echo addslashes($str) . this is safe in a database query.;
输出:
who's john adams? this is not safe in a database query.who\'s john adams? this is safe in a database query.
2、addcslashes(string,characters) 函数在指定的字符前添加反斜杠。stirng必须,第二个可选。规定受 addcslashes() 影响的字符或字符范围。
ps:在对 0,r,n 和 t 应用 addcslashes() 时要小心。在 php 中,\0,\r,\n 和 \t 是预定义的转义序列。此函数可以对任何字符,包括预定义字符进行反斜扛添加,这是和addslashes的区别
//向特定字符添加反斜杠$str = hello, my name is john adams.;echo $str;echo addcslashes($str,'m');echo addcslashes($str,'j');
输出:
hello, my name is john adams.hello, \my na\me is john ada\ms.hello, my name is \john adams.
//向字符串中的一个范围内的字符添加反斜杠$str = hello, my name is john adams.;echo $str;echo addslashes($str); //使用addslashesecho addcslashes($str,'a..z');echo addcslashes($str,'a..z');echo addcslashes($str,'a..h');
输出:
hello, my name is john adams.
hello, my name is john adams.
\hello, my name is \john \adams.
h\e\l\l\o, \m\y \n\a\m\e \i\s j\o\h\n a\d\a\m\s.
h\ello, my n\am\e is jo\hn a\d\ams.
下一篇:ubuntu下安装xampp
http://www.bkjia.com/phpjc/916818.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/916818.htmltecharticlephp中我经常容易混淆的三组函数,php混淆三组函数 原文:http://www.ido321.com/1252.html 一、htmlentities() 和htmlspecialchars() 1、htmlentities() 1.1 功能:...
其它类似信息

推荐信息