您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP效能集合类源码

2024/6/30 22:20:44发布44次查看
php功能集合类源码
代码如下:
$_value) { $_string[$_key] = tool::htmlstring($_value); //递归 } } elseif (is_object($_date)) { foreach ($_date as $_key=>$_value) { $_string->$_key = tool::htmlstring($_value); //递归 } } else { $_string = htmlspecialchars($_date); } return $_string; } /** * 数据库输入过滤 * @param string $_data * @return string */ static public function mysqlstring($_data) { $_data = trim($_data); return !gpc ? addcslashes($_data) : $_data; } /** * 清理session */ static public function unsession() { if (session_start()) { session_destroy(); } } /** * 验证是否为空 * @param string $str * @param string $name * @return bool (true or false) */ static function validateempty($str, $name) { if (empty($str)) { self::alertback('警告:' .$name . '不能为空!'); } } /** * 验证是否相同 * @param string $str1 * @param string $str2 * @param string $alert * @return js */ static function validateall($str1, $str2, $alert) { if ($str1 != $str2) self::alertback('警告:' .$alert); } /** * 验证id * @param number $id * @return js */ static function validateid($id) { if (empty($id) || !is_numeric($id)) self::alertback('警告:参数错误!'); } /** * 格式化字符串 * @param string $str * @return string */ static public function formatstr($str) { $arr = array(' ', ' ', '&', '@', '#', '%', '\'', '', '\\', '/', '.', ',', '$', '^', '*', '(', ')', '[', ']', '{', '}', '|', '~', '`', '?', '!', ';', ':', '-', '_', '+', '='); foreach ($arr as $v) { $str = str_replace($v, '', $str); } return $str; } /** * 格式化时间 * @param int $time 时间戳 * @return string */ static public function formatdate($time='default') { $date = $time == 'default' ? date('y-m-d h:i:s', time()) : date('y-m-d h:i:s', $time); return $date; } /** * 获得真实ip地址 * @return string */ static public function realip() { 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']); 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; } /** * 加载 smarty 模板 * @param string $html * @return null; */ static public function display() { global $tpl;$html = null; $htmlarr = explode('/', $_server[script_name]); $html = str_ireplace('.php', '.html', $htmlarr[count($htmlarr)-1]); $dir = dirname($_server[script_name]); $firststr = substr($dir, 0, 1); $endstr = substr($dir, strlen($dir)-1, 1); if ($firststr == '/' || $firststr == '\\') $dir = substr($dir, 1); if ($endstr != '/' || $endstr != '\\') $dir = $dir . '/'; $tpl->display($dir.$html); } /** * 创建目录 * @param string $dir */ static public function createdir($dir) { if (!is_dir($dir)) { mkdir($dir, 0777); } } /** * 创建文件(默认为空) * @param unknown_type $filename */ static public function createfile($filename) { if (!is_file($filename)) touch($filename); } /** * 正确获取变量 * @param string $param * @param string $type * @return string */ static public function getdata($param, $type='post') { $type = strtolower($type); if ($type=='post') { return tool::mysqlstring(trim($_post[$param])); } elseif ($type=='get') { return tool::mysqlstring(trim($_get[$param])); } } /** * 删除文件 * @param string $filename */ static public function delfile($filename) { if (file_exists($filename)) unlink($filename); } /** * 删除目录 * @param string $path */ static public function deldir($path) { if (is_dir($path)) rmdir($path); } /** * 删除目录及地下的全部文件 * @param string $dir * @return bool */ static public function deldirofall($dir) { //先删除目录下的文件: if (is_dir($dir)) { $dh=opendir($dir); while (!!$file=readdir($dh)) { if($file!=. && $file!=..) { $fullpath=$dir./.$file; if(!is_dir($fullpath)) { unlink($fullpath); } else { self::deldirofall($fullpath); } } } closedir($dh); //删除当前文件夹: if(rmdir($dir)) { return true; } else { return false; } } } /** * 验证登陆 */ static public function validatelogin() { if (empty($_session['admin']['user'])) header('location:/admin/'); } /** * 给已经存在的图片添加水印 * @param string $file_path * @return bool */ static public function addmark($file_path) { if (file_exists($file_path) && file_exists(mark)) { //求出上传图片的名称后缀 $ext_name = strtolower(substr($file_path, strrpos($file_path, '.'), strlen($file_path))); //$new_name='jzy_' . time() . rand(1000,9999) . $ext_name ; $store_path = root_path . updir; //求上传图片高宽 $imginfo = getimagesize($file_path); $width = $imginfo[0]; $height = $imginfo[1]; //添加图片水印 switch($ext_name) { case '.gif': $dst_im = imagecreatefromgif($file_path); break; case '.jpg': $dst_im = imagecreatefromjpeg($file_path); break; case '.png': $dst_im = imagecreatefrompng($file_path); break; } $src_im = imagecreatefrompng(mark); //求水印图片高宽 $src_imginfo = getimagesize(mark); $src_width = $src_imginfo[0]; $src_height = $src_imginfo[1]; //求出水印图片的实际生成位置 $src_x = $width - $src_width - 10; $src_y = $height - $src_height - 10; //新建一个真彩色图像 $nimage = imagecreatetruecolor($width, $height); //拷贝上传图片到真彩图像 imagecopy($nimage, $dst_im, 0, 0, 0, 0, $width, $height); //按坐标位置拷贝水印图片到真彩图像上 imagecopy($nimage, $src_im, $src_x, $src_y, 0, 0, $src_width, $src_height); //分情况输出生成后的水印图片 switch($ext_name) { case '.gif': imagegif($nimage, $file_path); break; case '.jpg': imagejpeg($nimage, $file_path); break; case '.png': imagepng($nimage, $file_path); break; } //释放资源 imagedestroy($dst_im); imagedestroy($src_im); unset($imginfo); unset($src_imginfo); //移动生成后的图片 @move_uploaded_file($file_path, root_path.updir . $file_path); } } /** * 中文截取2,单字节截取模式 * @access public * @param string $str 需要截取的字符串 * @param int $slen 截取的长度 * @param int $startdd 开始标记处 * @return string */ static public function cn_substr($str, $slen, $startdd=0){ $cfg_soft_lang = pagecharset; if($cfg_soft_lang=='utf-8') { return self::cn_substr_utf8($str, $slen, $startdd); } $restr = ''; $c = ''; $str_len = strlen($str); if($str_len $i+1) { $c = $str[$i].$str[$i+1]; } $i++; } else { $c = $str[$i]; } if($i >= $enddd) { if(strlen($restr)+strlen($c)>$slen) { break; } else { $restr .= $c; break; } } } return $restr; } /** * utf-8中文截取,单字节截取模式 * * @access public * @param string $str 需要截取的字符串 * @param int $slen 截取的长度 * @param int $startdd 开始标记处 * @return string */ static public function cn_substr_utf8($str, $length, $start=0) { if(strlen($str) $maxwidth) || ($maxheight && $pic_height > $maxheight)) { if($maxwidth && $pic_width>$maxwidth) { $widthratio = $maxwidth/$pic_width; $resizewidth_tag = true; } if($maxheight && $pic_height>$maxheight) { $heightratio = $maxheight/$pic_height; $resizeheight_tag = true; } if($resizewidth_tag && $resizeheight_tag) { if($widthratio0)) { $file_data = fread($fp, $buffer); $file_count += $buffer; echo $file_data; } fclose($fp); //关闭文件 }}?>
?
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product