PHP中htmlentities和htmlspecialchars函数的区别
做PHP程序的人都知道htmlentities和htmlspecialchars函数,都是格式化html代码的,但是这二个函数是否真的功能一样吗? php程序员之家 这两个函数在格式化带有英文字符的html代码的时候基本没啥问题,但是htmlentities对中文字符也不放过,这样得出来的结果是中文字符部分变为一堆乱码。当时做英文站的时候根本就没觉察到这个问题,而今天公司的一个收藏站却因为有有非英文字符而出现了问题,我最终查出来是 htmlentities这个函数的问题,同时我也找到了htmlspecialchars这个函数。
对于这两个函数,php手册上都是英文做的解释,其中在htmlentities函数的说明部分有这么一段英文:
This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.
从这句话中我们也可以看出来这两个函数虽然基本功能差不多,但是还是有细微的差别在里面的。再仔细看htmlspecialchars函数里面的一段话:
The translations performed are:
‘&' (ampersand) becomes ‘&'
‘”‘ (double quote) becomes ‘”‘ when ENT_NOQUOTES is not set.
”' (single quote) becomes ”' only when ENT_QUOTES is set. www.phperz.com
‘<' (less than) becomes ‘<'
‘>' (greater than) becomes ‘>' php程序员站 可以了解到htmlspecialchars只转化上面这几个html代码,而htmlentities却会转化所有的html代码,连同里面的它无法识别的中文字符也给转化了。
我们可以拿一个简单的例子来做比较: www~phperz~com 以下为引用的内容:
$str='<a href="test.html">测试页面</a>';
echo htmlentities($str); $str='<a href="test.html">测试页面</a>';
echo htmlspecialchars($str); 有条件的朋友,可以分别运行一下上面的这两段代码,就可以看出两者的差别了。 ___________________________________________________________________________________ htmlspecialchars把某些特殊字符转换成html的编码,常用到的场合可能就是处理客户留言的留言版了。 这些特殊字符仅限于以下几个: & -> &
相关文档:
服务器变量 $_SERVER 详解:
1、$_SESSION['PHP_SELF'] -- 获取当前正在执行脚本的文件名
2、$_SERVER['SERVER_PROTOCOL'] -- 请求页面时通信协议的名称和版本。例如,“HTTP/1.0”。
3、$_SERVER['REQUEST_TIME'] -- 请求开始时的时间戳。从 PHP 5.1.0 起有效。和time函数效果一样。
4、$_SERVER['a ......
PHP解疑
1. 代码重用方法include()和require()函数差异?
1) Require()函数
使用requier()包含外部php文件时,只要自身php文件被执行,外部文件的内容就将被包含进该自身php文件,当包含的外部文件发生错误时,系统将给出错误提示,并且停止php文件的执行。
示例:
调用文件config.inc的程序代码:
<?php ec ......
SQLite Tutorial in PHP
SQLite is an SQL database manager used locally or on a website, and compatible
in particularly with PHP.
Summary
Installing SQLite and creating a database
.
Installing SQLite. Verifying the installation by creating a base.
Creating and using a SQLite tabl ......
解决的办法有好几个:
第一个是:str_split(),这个方法是PHP5加入的。
<?php
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
输出就是:
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
......