php函数封装

这是一些自定义封装的函数类,调用起来很方便,以后会有更多的封装函数更新!

// 弹框跳转
function alert($msg,$url=''){
   echo "";
}


//获取单条数据
function getONE($sql){
   $sql = mysql_query($sql);
   $list = array();
   if($sql && mysql_affected_rows()>0){
      $list = mysql_fetch_assoc($sql);
   }return $list;
}

//获取多条数据
function getALL($sql){
   $sql = mysql_query($sql);
   $list = array();
   if($sql && mysql_affected_rows()>0){
      while($row = mysql_fetch_assoc($sql)){
      $list[] = $row;
      }
   }return $list;
}

/**
 * [delete 删除数据]
 * @param  [string] $table [表名]
 * @param  [string] $where [条件]
 * @return [boolean]       [返回结果]
 */
//删除数据SQL语句封装
function delete($table,$where){
   //构造删除的SQL语句
   $sql = "DELETE FROM `$table` WHERE $where;";   
   //把SQL语句执行
   $res = mysql_query($sql);
   //判断是否执行成功
   if($res && mysql_affected_rows()>0){
      return true;
   }else{
      return false;
   }
}

/**
 * [update 更新数据]
 * @param  [string] $table [表名]
 * @param  [array]  $array [要更新的数据]
 * @param  [string] $where [条件]
 * @return [type]        [返回结果]
 */
//更新数据SQL语句封装
function update($table,$array,$where){
   $str = '';
   foreach($array as $key => $value){
      $str .= "`".$key."`='".$value."',";
   }
   //去除最右边的逗号
   $str = rtrim($str,',');
   //构造更新的SQL语句
   $sql = "UPDATE `$table` SET $str WHERE $where";
   // dump($sql);exit;
   $res = mysql_query($sql);
   //判断是否执行成功
   if($res){
      return true;
   }else{
      return false;
   }
}

/**
 * [insert 插入数据]
 * @param  [string] $table [表名]
 * @param  [array] $data  [数据]
 * @return [bool]        [是否成功]
 */
function insert($table,$data){
   $tmp = array_keys($data);
   $field = '`'.implode('`,`', $tmp).'`';
   $value =  "'".implode("','",$data)."'";
   $sql = "INSERT INTO $table ($field) VALUES ($value)";
   // dump($sql);exit;
   $res = mysql_query($sql);
   if($res && mysql_affected_rows()>0){
      return mysql_insert_id();
   }else{
      return false;
   }
}

//pre预排版打印
function dump($content){
   echo "
";
   print_r($content);
   echo "
"; } /** * * 字符截取 * @param string $string * @param int $start * @param int $length * @param string $charset * @param string $dot * * @return string */ function str_cut(&$string, $start, $length, $charset = "utf-8", $dot = '...') { if(function_exists('mb_substr')) { if(mb_strlen($string, $charset) > $length) { return mb_substr ($string, $start, $length, $charset) . $dot; } return mb_substr ($string, $start, $length, $charset); }else if(function_exists('iconv_substr')) { if(iconv_strlen($string, $charset) > $length) { return iconv_substr($string, $start, $length, $charset) . $dot; } return iconv_substr($string, $start, $length, $charset); } $charset = strtolower($charset); switch ($charset) { case "utf-8" : preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $ar); if(func_num_args() >= 3) { if (count($ar[0]) > $length) { return join("", array_slice($ar[0], $start, $length)) . $dot; } return join("", array_slice($ar[0], $start, $length)); } else { return join("", array_slice($ar[0], $start)); } break; default: $start = $start * 2; $length = $length * 2; $strlen = strlen($string); for ( $i = 0; $i < $strlen; $i++ ) { if ( $i >= $start && $i < ( $start + $length ) ) { if ( ord(substr($string, $i, 1)) > 129 ) $tmpstr .= substr($string, $i, 2); else $tmpstr .= substr($string, $i, 1); } if ( ord(substr($string, $i, 1)) > 129 ) $i++; } if ( strlen($tmpstr) < $strlen ) $tmpstr .= $dot; return $tmpstr; }} /** * [uploads 文件上传函数] * @param [string] $name [表单域的name名] * @param [string] $catalog [文件保存的路径] * @param array $type [允许上传的文件类型] * @param integer $size [允许上传的文件大小] * @return [array] [error 1 上传失败 2 上传成功] */ function uploads($name,$catalog,$type=array('jpg','jpeg','gif','png'),$size=1048576){ $status = $_FILES[$name]['error']; if($status>0){ switch($status){ case 1: $res['msg'] = "文件上传超过最大值2M"; $res['err'] = 1; return $res; break; case 2: $res['msg'] = "文件上传超过MAX_FILE_SIZE大小"; $res['err'] = 1; return $res; break; case 3: $res['msg'] = "文件上传失败"; $res['err'] = 1; return $res; break; case 4: $res['msg'] = '请选择文件'; $res['err'] = 1; return $res; break; default: break; } } if($_FILES[$name]['size']>$size){ $res['msg'] = '上传文件超出指定大小'; $res['err'] = 1; return $res; } $ext = pathinfo($_FILES[$name]['name'],PATHINFO_EXTENSION); if(!in_array($ext,$type)){ $res['msg'] = '请上传指定的文件类型'; $res['err'] = 1; return $res; } //第一种做法 $catalog = rtrim($catalog,'/'); $dir = $catalog; if(!is_dir($dir)){ mkdir($dir,0777,true); } do{ $file = time().mt_rand(1000,9999); $filename = $file.'.'.$ext; $newname = $dir.'/'.$filename; }while(is_file($dir.'/'.$filename)); move_uploaded_file($_FILES[$name]['tmp_name'], $dir.'/'.$filename); $res['msg'] = '文件上传成功'; $res['err'] = 2; $res['filename'] = $filename; $res['name'] = $filename; return $res; } //封装缩略图的函数 function small($file,$widths,$heights,$address){ $filename="$file"; $info = getimagesize($filename); //获取图片的宽 $width = $info[0]; //获取图片的高 $height = $info[1]; //打开图片 if($info[2]==1){ $parent = imagecreatefromgif($filename); }else if($info[2]==2){ $parent = imagecreatefromjpeg($filename); }else if($info[2]==3){ $parent = imagecreatefrompng($filename); } $son_width = $widths; $son_height = $heights; // 等比例缩放 // $son_height = ceil(($height*$son_width)/$width); //w/h = s_w/s_h //新建图像 $son = imagecreatetruecolor($son_width,$son_height); imagecopyresized($son,$parent,0,0,0,0,$son_width,$son_height,$width,$height); $path = pathinfo($filename,PATHINFO_EXTENSION); $time = time(); $pathname = $time.mt_rand(1000,9999).'.'.$path; $save = $address.$pathname; if($info[2]==1){ imagegif($son,$save); }else if($info[2]==2){ imagejpeg($son,$save); }else if($info[2]==3){ imagepng($son,$save); } imagedestroy($son); imagedestroy($parent); return $pathname; } /** * 获得用户的真实IP地址 * * @access public * @return string */ function real_ip() { static $realip = NULL; if ($realip !== NULL) { return $realip; } if (isset($_SERVER)) { if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */ foreach ($arr AS $ip) { $ip = trim($ip); if ($ip != 'unknown') { $realip = $ip; break; } } } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $realip = $_SERVER['HTTP_CLIENT_IP']; } else { if (isset($_SERVER['REMOTE_ADDR'])) { $realip = $_SERVER['REMOTE_ADDR']; } else { $realip = '0.0.0.0'; } } } else { if (getenv('HTTP_X_FORWARDED_FOR')) { $realip = getenv('HTTP_X_FORWARDED_FOR'); } elseif (getenv('HTTP_CLIENT_IP')) { $realip = getenv('HTTP_CLIENT_IP'); } else { $realip = getenv('REMOTE_ADDR'); } } preg_match("/[\d\.]{7,15}/", $realip, $onlineip); $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0'; return $realip;} /** * [arraySort 无限极分类函数] * @param [type] $arr [description] * @param integer $parentid [description] * @return [type] [description] */ function arraySort($arr,$id,$pid,$parentid=0){ $list=array(); foreach($arr as $key => $v){ if($v[$pid]==$parentid){ $tmp = arraySort($arr,$id,$pid,$v[$id]); if($tmp){ $v['submenu'] = $tmp; } $list[]=$v; } } return $list; }

你可能感兴趣的:(php)