CanPHP开发手册--常用函数
说明:
- 下面介绍的常用函数位于CanPHP/lib/common.function.php文件中
- common.function.php文件默认已经加载,不需要手动include
- cp1.3版本将数据过滤函数文件filter.function.php合并到了common.function.php
函数:in($data,$force=false)
说明:用来过滤字符串和字符串数组,防止被挂马和sql注入,字符串过滤用in()函数,数字过滤采用php自带的intval()函数。
参数:
- $data,待过滤的字符串或字符串数组。
- $force为true,忽略get_magic_quotes_gpc,用于过滤非cookie,get,post提交的数据
函数:out($data)
说明:用来还原字符串和字符串数组,把已经转义的字符还原回来
参数:
- $data:字符串或数组。
函数:text_in($str)
说明:文本输入,用于textarea文本区域的内容换行和空格处理
参数:
- $str:字符串
函数:text_out($str)
说明:文本输出,用于textarea文本区域的内容换行和空格处理
参数:
- $str:字符串
函数: html_in($str)
说明:html代码输入,用于在线编辑器提交的数据过滤,
注意此函数会过滤掉iframe和js代码,且html_out()不能还原js代码
注意此函数会过滤掉iframe和js代码,且html_out()不能还原js代码
参数:
- $str:含html代码的字符串。
函数:html_out($str)
说明:html代码输出,用于还原过滤后的文章内容
参数:
- $str:经过转义后的html代码字符串
函数:get_client_ip()
说明:获取客户端ip地址
参数:
- 如果能成功获取ip地址,返回ip地址,否则返回‘unknown’。
使用方法:
$ip=get_client_ip();
函数:msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
说明:中文字符串截取
参数:
- $str:要截取的字符串。
- $start:开始截取的字符,一般从0开始。
- $length:截取的字符长度,按字符计算,不是按字节计算。
- $charset:字符串的编码。utf-8,gbk,gb2312,big5
- $suffix:字符串过长,截断之后是否显示”……“。
使用方法:
msubstr($str, 0, 20);//截取20个字符
函数:is_email($user_email)
说明:检查是否是正确的邮箱地址,是则返回true,否则返回false
参数:
- $user_email:给定的邮箱地址。
使用方法:
if(!is_email($user_email)) { echo '邮箱地址不正确'; }
函数:module($module)
说明:模块调用
参数:
- $module:模块名称
- 返回模块实例化对象
使用方法:
module('article')->getList();//调用article模块的getList方法
函数:is_utf8($string)
说明: 检查字符串是否是UTF8编码,是返回true,否则返回false
参数:
- $string:要判断的字符串。
使用方法:
//如果数据,不是utf8编码,则转换成utf-8编码 if(!is_utf8($str)) { $str=auto_charset($str,'gbk','utf-8'); }
函数:auto_charset($fContents,$from='gbk',$to='utf-8')
说明:编码转换,常用于解决乱码问题
参数:
- $fContents:要转换的字符串或数组
- $from:数据原始编码
- $to:需要转换成的编码
使用方法:
$data=auto_charset($data,'gbk','utf-8');//gbk编码转成utf-8编码 $data=auto_charset($data,'utf-8','gbk');//utf-8编码转成gbk编码
函数:dump($var, $exit=false)
说明:浏览器友好的变量输出,常用于调试
参数:
- $var:要输出的字符串或数组。
- $exit:输出数据之后,是否执行exit,退出程序
使用方法:
dump($data);//输出数据 dump($data,true);//输出数据,并退出程序,不再执行下面的代码
函数:utime()
说明:获取微秒时间,常用于计算程序的运行时间
参数:
- 参数为空
- 返回当前时间戳+微秒时间
函数:cp_uniqid()
说明:生成唯一的值,常用于文件上传时的文件命名
参数:
- 参数为空
- 返回32位的字符串
函数:cp_encode($data,$key='',$expire = 0))
说明:加密函数,可用cp_decode()函数解密
参数:
- $data:待加密的字符串或数组;
- $key:密钥;
- $expire 过期时间
- 返回加密后的字符串
函数:cp_decode($string,$key='')
说明:解密函数,解密经cp_encode之后的密文
参数:
- $string:待解密的字符串
- $key:密钥
函数:json_encode($data)
说明:如果json_encode没有定义,则定义json_encode函数,常用于返回ajax数据
参数:
- $data:数组
函数:del_dir($dir)
说明:遍历删除目录和目录下所有文件,可用于删除缓存
参数:
- $dir:要删除的目录,字符串
函数:model($model)
说明:模型调用
参数:
- $model:模型名称
- 返回模型实例化对象