Google
      
发新话题
打印

[教程] 从UCH里提取出来的函数,可重用

从UCH里提取出来的函数,可重用

基本上每个程序员都有自己的“代码库”,或者一些封装的类,这是一个好的习惯,不仅缩短以后的开发时间,而且代码都是出自高手之手。
复制内容到剪贴板
代码:
<?php
/***********检查邮箱格式*****************/
function isemail($email) {
        return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
}

/***********用header完成文件下载*****************/
function download($filename){
  $size   =  filesize($filename);
  header("Content-length:$size");   
  header("Content-type:application/force-download ");   
  header("Content-Description: PHP3 Generated Data");
  header("Content-disposition:attachment;filename=".basename($filename));   
  readfile($filename);
}

/***********获取目录*************/
function sreaddir($dir, $extarr=array()) {
        $dirs = array();
        if($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                        if(!empty($extarr) && is_array($extarr)) {
                                if(in_array(strtolower(fileext($file)), $extarr)) {
                                        $dirs[] = $file;
                                }
                        } else if($file != '.' && $file != '..') {
                                $dirs[] = $file;
                        }
                }
                closedir($dh);
        }
        return $dirs;
}

/***********递归清空目录*************/
function deltreedir($dir) {
        $files = sreaddir($dir);
        foreach ($files as $file) {
                if(is_dir("$dir/$file")) {
                        deltreedir("$dir/$file");
                } else {
                        @unlink("$dir/$file");
                }
        }
}

/***********获取文件内容*************/
function sreadfile($filename) {
        $content = '';
        if(function_exists('file_get_contents')) {
                @$content = file_get_contents($filename);
        } else {
                if(@$fp = fopen($filename, 'r')) {
                        @$content = fread($fp, filesize($filename));
                        @fclose($fp);
                }
        }
        return $content;
}

/***********写入文件***********/
function swritefile($filename, $writetext, $openmod='w') {
        if(@$fp = fopen($filename, $openmod)) {
                flock($fp, 2);
                fwrite($fp, $writetext);
                fclose($fp);
                return true;
        } else {
                runlog('error', "File: $filename write error.");
                return false;
        }
}

/***********产生随机字符***********/
function random($length, $numeric = 0) {
        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
        if($numeric) {
                $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
                $hash = '';
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
                $max = strlen($chars) - 1;
                for($i = 0; $i < $length; $i++) {
                        $hash .= $chars[mt_rand(0, $max)];
                }
        }
        return $hash;
}

/***********数组array('a','b')转换成字串array('a','b'),格式不变***********/
function arrayeval($array, $level = 0) {
        $space = '';
        for($i = 0; $i <= $level; $i++) {
                $space .= "\t";
        }
        $evaluate = "Array\n$space(\n";
        $comma = $space;
        foreach($array as $key => $val) {
                $key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
                $val = !is_array($val) && (!preg_match("/^\-?\d+$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
                if(is_array($val)) {
                        $evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
                } else {
                        $evaluate .= "$comma$key => $val";
                }
                $comma = ",\n$space";
        }
        $evaluate .= "\n$space)";
        return $evaluate;
}

/***********递归添加转义符***********/
function saddslashes($string) {
        if(is_array($string)) {
                foreach($string as $key => $val) {
                        $string[$key] = saddslashes($val);
                }
        } else {
                $string = addslashes($string);
        }
        return $string;
}

/***********递归去掉转义符***********/
function sstripslashes($string) {
        if(is_array($string)) {
                foreach($string as $key => $val) {
                        $string[$key] = sstripslashes($val);
                }
        } else {
                $string = stripslashes($string);
        }
        return $string;
}

/***********递归取消HTML代码***********/
function shtmlspecialchars($string) {
        if(is_array($string)) {
                foreach($string as $key => $val) {
                        $string[$key] = shtmlspecialchars($val);
                }
        } else {
                $string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
                        str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string));
        }
        return $string;
}

/****************取得客户端ip地址****************/
function getip(){
if (getenv("http_client_ip") && strcasecmp(getenv("http_client_ip"), "unknown"))
$ip = getenv("http_client_ip");
else if (getenv("http_x_forwarded_for") && strcasecmp(getenv("http_x_forwarded_for"), "unknown"))
$ip = getenv("http_x_forwarded_for");
else if (getenv("remote_addr") && strcasecmp(getenv("remote_addr"), "unknown"))
$ip = getenv("remote_addr");
else if (isset($_server[@#remote_addr@#]) && $_server[@#remote_addr@#] && strcasecmp($_server[@#remote_addr@#], "unknown"))
$ip = $_server[@#remote_addr@#];
else
$ip = "unknown";
return($ip);
}
?>

TOP

回复 1# doublec 的帖子

谢谢!来研究一下吧!

TOP

回复 1# doublec 的帖子

谢谢!来研究一下吧!

TOP

发新话题