PHP 之 面试题(笔试题总结)

多个进程同时处理同一文件,加下锁防冲突

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);//读取文件
        //echo $_current;exit;
        $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){
    //$file  = './qq.txt';
    $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");   //从最后一个\n开始一直到末尾的字符串
        $last_n_len     = strlen($last_n);            //从最后一个\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;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

数据库分表(提前分)

//生成id
    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;
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

文件查找函数

//文件查找函数
//返回文件绝对路径
//PHP_EOL换行符
function guchi($name="*",$type="*"){
    $files = glob("{$name}.{$type}");
    //realpath
    foreach ($files as $filename){
        $baf[] = realpath($filename);         // .filesize($filename);
    }
    return $baf;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

图片放大或压缩函数

/**图片放大或压缩函数
*
*$imgname
*$sizex,$sizey 相对原图宽高比例
*$xsize="",$ysize="" 新图宽高值(设置后宽高比例失效)
*return 新图名字
*/

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);//创建一块画布,并从 PNG 文件或 URL 地址载入一副图像
        break;

        case "gif":
            $img=imagecreatefromgif($imgname);//创建一块画布,并从 GIF 文件或 URL 地址载入一副图像
        break;

        case "jpg":
            $img=imagecreatefromjpeg($imgname); //获得源文件
        break;
    }

    //echo $ext;exit("hui");
    $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);
    //(0,0,0,0)前两个是新文件的起始坐标,后面两个是源文件的起始坐标
    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对象转化为数组

//将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;
        }
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

将数组转化为对象

//将数组转化为对象
    function arrayToObject($d) { 
        if (is_array($d)) { 
            return (object) array_map(__FUNCTION__, $d);
        }else{return $d;}
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

字符串截取函数

//字符串截取函数
    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位用来保存时间戳,解密时验证数据有效性,1026位用来保存$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));   
    }   
}  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值