多个进程同时处理同一文件,加下锁防冲突
function writeData($path, $mode, $data){
$fp = fopen($path, $mode);
$retries = 0;
$max_retries = 100;
do{
if ($retries > 0){
usleep(rand(1, 10000));
}
$retries += 1;
}while(!flock($fp, LOCK_EX) and $retries <= $max_retries);
if ($retries == $max_retries) {
return false;
}
fwrite($fp, "$data\n");
flock($fp, LOCK_UN);
fclose($fp);
return true;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
超大文件读取
error_reporting(E_ALL^E_NOTICE);
function readBigFile($filename, $count = 20, $tag = "\r\n") {
$content = "";
$_current = "";
$step= 1;
$tagLen = strlen($tag);
$start = 0;
$i = 0;
$handle = fopen($filename,'r+');
while($i < $count && !feof($handle)) {
fseek($handle, $start, SEEK_SET);
$_current = fread($handle,$step);
$content .= $_current;
$start += $step;
$substrTag = substr($content, -$tagLen);
if ($substrTag == $tag) {
$i++;
}
}
fclose($handle);
return $content;
}
$filename = 'F:/wamp/www/test/import_order_log.txt';
$tag = "\r\n";
$count = 20;
$data = readBigFile($filename,$count,$tag);
echo $data;
exit;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
多维数组转一维
function arrToOne($multi) {
$arr = array();
foreach ($multi as $key => $val) {
if( is_array($val) ) {
$arr = array_merge($arr, arrToOne($val));
} else {
$arr[] = $val;
}
}
return $arr;
}
function get_real_ip(){
$ip=false;
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ips=explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
if($ip){ array_unshift($ips, $ip); $ip=FALSE; }
for ($i=0; $i < count($ips); $i++){
if(!eregi ('^(10│172.16│192.168).', $ips[$i])){
$ip=$ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
echo get_real_ip();
echo $_SERVER['REMOTE_ADDR'];
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
拆分成小文件
function chai($file){
$sub_file_count = 100;
$sub_file_len = ceil(filesize($file)/$sub_file_count);
$handle = fopen($file, "rb");
rewind($handle);
$last_n_len = 0;
for ($i=0;$i<$sub_file_count;$i++){
$content = fread($handle , $sub_file_len + $last_n_len);
$last_n = strrchr($content , "\n");
$last_n_len = strlen($last_n);
$trim_n_content = substr($content , 0 , strlen($content) - $last_n_len);
fwrite(fopen($i.".log", "wb") , $trim_n_content);
fseek($handle , ftell($handle) - $last_n_len + 1);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
功能:删除数组出现次数最多或最少
function dodellargemsl($array){
$count = array_count_values($array);
$max= max($count);
foreach($count as $kt=>$vw){
if($vw==$max){
$vc[]=$kt;
}
}
foreach($array as $k=>$v){
if( in_array($v,$vc)){
unset($array[$k]);
}
}
return $array;
}
数据库分表(提前分)
function get_AI_ID($canshu) {
$sql = "insert into create_id (id) values('')";
$sid = $canshu->query($sql);
return $canshu->lastInsertId();
}
function new_Article($canshu) {
$id = get_AI_ID($canshu);
$table_name = get_Table_Name($id);
$sql = "insert into {$table_name} (id,subject,content) values('{$id}','测试标题','测试内容')";
print_r($id);
echo $table_name;
$canshu->exec($sql);
}
function get_Table_Name($id) {
return 'article_'.intval($id)%10;
}
$opts_values = array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8');
$dns = "mysql:host=localhost;dbname=ttlsa_com";
$canshu=$dbObj = new PDO($dns,'root','',$opts_values);
$dbObj -> query('set names utf-8');
new_Article($canshu);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
excel文档导入
function mslz(){
echo "<pre>";
//$data = file_get_contents('./test.csv');
//$dataa = @explode("",$data);
$datab = fopen('./test.csv','r');
while (!feof($datab)) {
$datae[] = fgets($datab);
}
fclose($datab);
$datae = array_filter($datae);
unset($datae[0]);
foreach($datae as $v){
echo $v;
//var_dump($v);
$r = rtrim($v,",
");//特别之处
//$r = rtrim($v,",");
// echo $r=;
echo $r.PHP_EOL;
}
//print_r(trim("9.78076E+12,95,",","));
//print_r($datae);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
判断参数中是否含有中文
function has_chinese($address) {
if (!$address) {
return false;
}
if (preg_match("/[\x7f-\xff]/", trim($address))) {
$has_chinese = true;
} else {
$has_chinese = false;
}
return $has_chinese;
}
文件查找函数
function guchi($name="*",$type="*"){
$files = glob("{$name}.{$type}");
foreach ($files as $filename){
$baf[] = realpath($filename);
}
return $baf;
}
图片放大或压缩函数
function doimage($imgname,$sizex,$sizey,$xsize="",$ysize=""){
$ext = pathinfo($imgname,PATHINFO_EXTENSION);
$type=array("jpg","jpeg","png","gif");
if(!in_array($ext,$type)){
exit("格式错误");
}
$ext = strtolower($ext);
switch($ext){
case "jpeg":
$img=imagecreatefromjpeg($imgname);
break;
case "png":
$img=imagecreatefrompng($imgname);
break;
case "gif":
$img=imagecreatefromgif($imgname);
break;
case "jpg":
$img=imagecreatefromjpeg($imgname);
break;
}
$x=imagesx($img);
$y=imagesy($img);
$newx=$xsize?$xsize:$x*$sizex;
$newy=$ysize?$ysize:$y*$sizey;
$newimg = imagecreatetruecolor($newx, $newy);
imagecopyresized ($newimg,$img,0,0,0,0,$newx,$newy,$x,$y);
if($ext=="jpg"||$ext=="jpeg"){
$newname = date("Y-m-d-H-i-s",time());
header ("Content-type: image/jpeg");
imagejpeg ($newimg,"{$newname}.jpg");
return $newname."jpg";
}
if($ext=="png"){
$newname = date("Y-m-d-H-i-s",time());
header ("Content-type: image/png");
imagejpeg ($newimg,"{$newname}.png");
return $newname."png";
}
if($ext=="gif"){
$newname = date("Y-m-d-H-i-s",time());
header ("Content-type: image/gif");
imagejpeg ($newimg,"{$newname}.gif");
return $newname."gif";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
将php对象转化为数组
function objectToArray($obj) {
if(is_object($obj)){
$obj = get_object_vars($obj);
}
if(is_array($obj)){
return array_map(__FUNCTION__, $obj);
}else{
return $obj;
}
}
将数组转化为对象
function arrayToObject($d) {
if (is_array($d)) {
return (object) array_map(__FUNCTION__, $d);
}else{return $d;}
}
字符串截取函数
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
if(function_exists("mb_substr")){
if($suffix)
return mb_substr($str, $start, $length, $charset);
else
return mb_substr($str, $start, $length, $charset);
}
elseif(function_exists('iconv_substr')) {
if($suffix)
return iconv_substr($str,$start,$length,$charset);
else
return iconv_substr($str,$start,$length,$charset);
}
$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));
if($suffix) return $slice;
return $slice;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
表单提交框处理函数
function checkstr($str){
$str= trim($str);
$str = htmlspecialchars($str);
$str = strtolower($str);
$str=str_replace("join","",$str);
$str=str_replace("union","",$str);
$str=str_replace("select","",$str);
$str=str_replace("where","",$str);
$str=str_replace("insert","",$str);
$str=str_replace("delete","",$str);
$str=str_replace("update","",$str);
$str=str_replace("like","",$str);
$str=str_replace("drop","",$str);
$str=str_replace("create","",$str);
$str=str_replace("modify","",$str);
$str=str_replace("rename","",$str);
$str=str_replace("alter","",$str);
$str=str_replace("admin","",$str);
$str=str_replace("performance_schema","",$str);
$str=str_replace("mysql","",$str);
$str = preg_replace( "@<script(.*?)</script>@is", "", $str );
$str = preg_replace( "@<iframe(.*?)</iframe>@is", "", $str );
$str = preg_replace( "@<style(.*?)</style>@is", "", $str );
$str = preg_replace( "@<(.*?)>@is", "", $str );
return $str;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
加密解密函数
// 加密解密函数
// $string: 明文 或 密文
// $operation:DECODE表示解密,其它表示加密
// $key: 密匙
// $expiry:密文有效期
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
// 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙
$ckey_length = 4;
// 密匙
$key = md5($key ? $key : $GLOBALS['discuz_auth_key']);
// 密匙a会参与加解密
$keya = md5(substr($key, 0, 16));
// 密匙b会用来做数据完整性验证
$keyb = md5(substr($key, 16, 16));
// 密匙c用于变化生成的密文
$keyc = $ckey_length ? ($operation == 'DECODE'
? substr($string, 0, $ckey_length): substr(md5
(microtime()), -$ckey_length)) : '';
//PHP加密解密函数authcode参与运算的密匙
$cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey);
// 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b),解密时会通过这个密匙验证数据完整性
// 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确
$string = $operation == 'DECODE' ? base64_decode(substr
($string, $ckey_length)) : sprintf('%010d', $expiry ?
$expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
//PHP加密解密函数authcode产生密匙簿
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
// 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
//PHP加密解密函数authcode核心加解密部分
for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
// PHP加密解密函数authcode从密匙簿得出密匙进行异或,再转成字符
$result .= chr(ord($string[$i]) ^ (
$box[($box[$a] + $box[$j]) % 256]));
}
if($operation == 'DECODE') {
// substr($result, 0, 10) == 0 验证数据有效性
// substr($result, 0, 10) - time() > 0 验证数据有效性
// substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16) //验证数据完整性
// 验证数据有效性,请看未加密明文的格式
if((substr($result, 0, 10) == 0 ||
substr($result, 0, 10) - time() > 0) &&
substr($result, 10, 16) == substr(md5
(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
//PHP加密解密函数authcode把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因
// 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码
return $keyc.str_replace('=', '',
base64_encode($result));
}
}