20171007

作者: 雨y飘零久 | 来源:发表于2017-10-08 10:46 被阅读0次

    1.PHP遍历一个文件夹下所有文件和子文件夹的函数

    function my_dir($dir = '.') {
        $files = array();
        if($handle = opendir($dir)) { //注意这里要加一个@,不然会有warning错误提示:)
            while(($file = readdir($handle)) !== false) {
                if($file != ".." && $file != ".") { //排除根目录;
                    if(is_dir($dir."/".$file)) { //如果是子文件夹,就进行递归
                        $files[$file] = my_dir($dir."/".$file);
                    } else { //不然就将文件的名字存入数组;
                        $files[] = $file;
                    }
    
                }
            }
            closedir($handle);
            return $files;
        }
    }
    echo "<pre>";
    print_r(my_dir("."));
    echo "</pre>";
    

    2.找出有序数组中随机3个数和为0的所有情况

    <?php
    function three_sum($arr) {
      $n = count($arr);
      $result = array();
    
      for ($i=0; $i < $n; $i++) {
        $left = $i + 1;
        $right = $n - 1;
    
        while ($left <= $right) {
          $sum = $arr[$i]+$arr[$left]+$arr[$right];
          if($sum < 0){
            $left++;
          }else if($sum > 0){
            $right--;
          }else{
            $numbers = $arr[$i] . ',' . $arr[$left] . ',' . $arr[$right];
            if(!in_array($numbers, $result)){
              $result[] = $numbers;
            }
            $left++;
            $right--;
          }
        }
      }
      return $result;
    }
    $array = array(-12, -8, -5, -3, -1, 0, 1, 2, 4, 5, 7, 9, 12);
    var_dump(three_sum($array));
    
    E:\wamp64\www\demo\20171002demo\demo5.php:29:
    array (size=10)
      0 => string '-12,0,12' (length=8)
      1 => string '-12,5,7' (length=7)
      2 => string '-8,-1,9' (length=7)
      3 => string '-8,1,7' (length=6)
      4 => string '-8,4,4' (length=6)
      5 => string '-5,0,5' (length=6)
      6 => string '-5,1,4' (length=6)
      7 => string '-3,-1,4' (length=7)
      8 => string '-3,1,2' (length=6)
      9 => string '-1,0,1' (length=6)
    

    3.四种常见的 POST 提交数据方式:

    • application/x-www-form-urlencoded

      • 提交的数据按照 key1=val1&key2=val2 的方式进行编码,key 和 val 都进行了 URL 转码
    • multipart/form-data

      • 这种方式一般用来上传文件
    • application/json

      • 序列化后的 JSON 字符串
    • text/xml

      • XML 作为编码方式的远程调用规范

    4.get_defined_constants()

    获取所有PHP常量

    5.闭包调用时绑定(php7新特性)

    PHP 5.4版本时,提供了Closure->bindTo()和Closure::bind()方法,用于改变闭包中$this的绑定对象和作用域,同时复制一个新的闭包。
    PHP 7现在加入了一个更加简单的方法Closure->call(),使其在调用时绑定$this和作用域。此方法将绑定的对象作为第一个参数,其后的参数作为闭包参数传入,如下:

    <?php
    class HelloWorld{
      private $greeting  = "Hello";
    }
    
    $closure = function($whom) { echo $this->greeting . ' ' . $whom; }
    
    $obj = new HelloWorld();
    $closure->call($obj, 'World'); // Hello World
    

    6.列出一些防范SQL注入、XSS攻击、CSRF攻击的方法

    7..写一个正则表达式,过滤JS脚本(及把script标记及其内容都去掉)

    <?php
    $text = "<script>alert('XSS')</script>1112";
    $pattern = '/<script[^>]*?>.*?<\/script>/si';
    $text = preg_replace($pattern, '', $text);
    var_dump($text);
    

    E:\wamp64\www\demo\20171002demo\demo5.php:5:string '1112' (length=4)

    相关文章

      网友评论

          本文标题:20171007

          本文链接:https://www.haomeiwen.com/subject/ktbdyxtx.html