常用函数php
//随机数 /** * @param int $length 随机数长度 * @param string $num 是否只包含数字 * @return null|string 返回随机字符串 */ function codestr($length=4,$num=''){ $str = null; $strPol = $num==1 ? '123456789' : "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; $max = strlen($strPol)-1; for($i=0;$i<$length;$i++){ $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数 } return $str; } /** * @param $obj * @return mixed * 对象转换为数组 */ function objToArray($obj){ return json_decode(json_encode($obj),true); } /** * 字符串截取 * @param $str * @param int $start * @param $length * @param string $charset * @param bool|true $suffix * @return string */ function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) { if(function_exists("mb_substr")) $slice = mb_substr($str, $start, $length, $charset); elseif(function_exists('iconv_substr')) { $slice = iconv_substr($str,$start,$length,$charset); if(false === $slice) { $slice = ''; } }else{ $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; preg_match_all($re[$charset], $str, $match); $slice = join("",array_slice($match[0], $start, $length)); } return $suffix ? $slice.'...' : $slice; } /** * @param $list * @param string $pk * @param string $pid * @param string $child * @param int $root * @return array * 无限分类 数组转 树形结构 */ function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) { // 创建Tree $tree = array(); if(is_array($list)) { // 创建基于主键的数组引用 $refer = array(); foreach ($list as $key => $data) { $refer[$data[$pk]] =& $list[$key]; } foreach ($list as $key => $data) { // 判断是否存在parent $parentId = $data[$pid]; if ($root == $parentId || (isset($data['spid'])&&$data['pid'].'|' == $data['spid'])) { $tree[$data[$pk]] =& $list[$key]; }else{ if (isset($refer[$parentId])) { $parent =& $refer[$parentId]; $parent[$child][$data[$pk]] =& $list[$key]; } } } } return $tree; } /** * @param $str * @return string * 密码加密 */ function passwordEncode($str,$code=''){ return sha1($code.md5($str)); } /** * @param $mobile * @return bool * 验证手机号码是否正确 */ function is_mobile($mobile){ $reg = '/^1[34578][0-9]{9}$/'; if(!preg_match($reg,$mobile)){ return false; } return true; } function isUrl($url) { $reg = '/^http[s]?:\/\/[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*$/'; if(!preg_match($reg,$url)){ return false; } return true; } function isEmail($email){ return filter_var($email,FILTER_VALIDATE_EMAIL); }
/** * 将数组转换为字符串 * * @param array $data 数组 * @param bool $isformdata 如果为0,则不使用new_stripslashes处理,可选参数,默认为1 * @return string 返回字符串,如果,data为空,则返回空 */ function array2string($data, $isformdata = 1) { if($data == '') return ''; if($isformdata) $data = new_stripslashes($data); return addslashes(var_export($data, TRUE)); } function new_addslashes($string){ if(!is_array($string)) return addslashes($string); foreach($string as $key => $val) $string[$key] = new_addslashes($val); return $string; } /** * 返回经stripslashes处理过的字符串或数组 * @param $string 需要处理的字符串或数组 * @return mixed */ function new_stripslashes($string) { if(!is_array($string)) return stripslashes($string); foreach($string as $key => $val) $string[$key] = new_stripslashes($val); return $string; } /** * 创建目录 * * @param string $path 路径 * @param string $mode 属性 * @return string 如果已经存在则返回true,否则为flase */ function dir_create($path, $mode = 0777) { if(is_dir($path)) return TRUE; $ftp_enable = 0; $path = dir_path($path); $temp = explode('/', $path); $cur_dir = ''; $max = count($temp) - 1; for($i=0; $i<$max; $i++) { $cur_dir .= $temp[$i].'/'; if (@is_dir($cur_dir)) continue; @mkdir($cur_dir, 0777,true); @chmod($cur_dir, 0777); } return is_dir($path); } function dir_path($path) { $path = str_replace('\\', '/', $path); if(substr($path, -1) != '/') $path = $path.'/'; return $path; } /** * 取得文件扩展 * * @param $filename 文件名 * @return 扩展名 */ function fileext($filename) { return strtolower(trim(substr(strrchr($filename, '.'), 1, 10))); } /** * 返回经htmlspecialchars处理过的字符串或数组 * @param $obj 需要处理的字符串或数组 * @return mixed */ function new_html_special_chars($string) { $encoding = 'utf-8'; //if(strtolower(CHARSET)=='gbk') $encoding = 'ISO-8859-15'; if(!is_array($string)) return htmlspecialchars($string,ENT_QUOTES,$encoding); foreach($string as $key => $val) $string[$key] = new_html_special_chars($val); return $string; }