PHP 常用函数

    1. substr_count 统计子串在目标字符串中出现的次数
      $haystack = "this is a text";
      $needle = "is";
      $ret = substr_count($haystack, $needle); // 2
      $ret2 = substr_count($haystack, $needle, 3); // 1
      $ret3 = substr_count($haystack, $needle, 3, 4);// 1
      $ret4 = substr_count($haystack, $needle, -8); // 0
      $ret5 = substr_count($haystack, $needle, -9);
      $ret6 = substr_count($haystack, $needle, -9, 1); // 0
      $ret7= substr_count($haystack, $needle, -9, 2); // 1
    1. str_repeat 重复目标字符串指定次数
      $input = '|--';
      echo str_repeat($input, 4);// |--|--|--|--
      echo str_repeat($input, 1);// |--
      echo str_repeat($input, 0);// ""
      echo str_repeat($input, -1);// error
    1. compact 创建一个包扩变量名和变量值的数组
      $first = 'one';
      $second = 'two';
      print_r(compact('first', 'second'));//Array('first' => 'one', 'second' => 'two')
    1. count 统计数组内元素的个数
      $arr1 = ['a', 'b', 'c'];
      $arr2 = ['arr1' => $arr1];
      var_dump(count($arr1));// 3
      var_dump(count($arr2));// 1
      var_dump(count($arr2, COUNT_RECURSIVE));// 4
    1. is_array 检测变量是否是数组类型
      $arr = array(1,2,3,4,5);
      $str = 'hello world!';
      var_dump(is_array($arr);// true
      var_dump(is_array($str));// false
    1. substr 返回字符串中的子串
      $input = "abcdefg";
      $ret = substr($input, 0); // abcdefg
      $ret2 = substr($input, 2); //cdefg
      $ret3 = substr($input, 2, 3); //cde
      $ret4 = substr($input, 2, -1); //cdef
    1. in_array 检测数组中是否存在某个值
      $arr = ['hello', 'apple', 'color', 20];
      in_array('hello', $arr);// true
      in_array('Hello', $arr);// false
      in_array('20', $arr);// true
      in_array('20', $arr, false);// true
      in_array('20', $arr, true);// false
    1. explode 使用一个字符串分割另一个字符串
      $pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
      $ret = explode(' ', $pizza);
      $ret2 = explode(' ', $pizza, 1);
      $ret3 = explode(' ', $pizza, 2);
      $ret4 = explode(' ', $pizza, -1);
      $ret5 = explode(' ', $pizza, 0);
    1. implode 将一维数组各个元素用指定字符拼接成一个字符串
      $arr = ['apple', 'banana', 'canada', 'dog', 'emoja'];
      $ret = implode(',', $arr);
      $ret2 = implode($arr);
    1. strlen 统计字符串长度
      $str1 = null;// 0
      $str2 = '';// 0
      $str3 = 'hello';// 5
    1. str_replace 子字符串替换
      $vowels = array("a", "e", "i", "o", "u", "A", "E");
      $onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
      $phrase  = "You should eat fruits, vegetables, and fiber every day.";
      $healthy = array("fruits", "vegetables", "fiber");
      $yummy   = array("pizza", "beer", "ice cream");
      $newphrase = str_replace($healthy, $yummy, $phrase);
      $str = str_replace("ll", "", "good golly miss molly!", $count);
    1. array_merge 合并一个或多个数组
      $array1 = array("color" => "red", 2, 4);
      $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
      $ret = array_merge($array1, $array2);
    1. array_combine 合并两个数组
      $arr_key = ['first', 'second', 'three'];
      $arr_val = [1,2,3];
      $ret = array_combine($arr_key, $arr_val);
    1. strpos 查找子字符串首次出现的位置
      $haystack = 'abcdefg';
      $ret = strpos($haystack, 'b'); // 1
      $ret2 = strpos($haystack, 'B'); // false
      $ret3 = strpos($haystack, 'b', 2); //false
    1. sprintf 将向格式化字符串中写入变量
      $number = 10;
      $str = "RUNOOB";
      $txt = sprintf("%s 每天有 %u 万人在访问%b!",$str,$number,10);
      echo $txt;
      echo '<br />';
      $number = 123;
      $txt = sprintf("带有两位小数:%.2f
      <br>不带小数:%1\$u hello %1\$u",$number);  //'1\$'表示占位符
      echo $txt;
      //输出结果
      RUNOOB 每天有 10 万人在访问1010!
      带有两位小数:123.00 
      不带小数:123 hello 123
    1. trim 去除字符串首尾处的空白符
      $string = "abcd.* ";
      $ret = trim($string); // 'abcd.*'
      $ret2 = trim($string, ''); // 'abcd.* '
    1. strtolower(strtoupper) 将字符串中的所有字符转为小写字母(大写字母)
      $str = 'lkasdjflKDSflkasjdlaksdfjl';
      echo $str;
      echo '<br />';
      echo strtolower($str);
      echo '<br />';
      echo strtoupper($str);
      //输出结果
      lkasdjflKDSflkasjdlaksdfjl
      lkasdjflkdsflkasjdlaksdfjl
      LKASDJFLKDSFLKASJDLAKSDFJL
    1. array_keys 获取数组内的键值
      $arr = array(1,2,3,4,5,'3');
      $arr2 = array_keys($arr);
      print_r($arr2);
      echo '<br />';
      $arr3 = array_keys($arr, 3);
      print_r($arr3);
      echo '<br />';
      $arr4 = array_keys($arr, 3, true);
      print_r($arr4);
      //输出结果
      Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 ) 
      Array ( [0] => 2 [1] => 5 ) 
      Array ( [0] => 2 )
    1. array_key_exists 检查数组中是否存在指定的键名或索引
      $arr = array(1,2,3,4,5,6);
      $res = array_key_exists(0, $arr);
      var_dump($res);
      echo '<br />';
      $res2 = array_key_exists(10, $arr);
      var_dump($res2);
      //输出结果
      bool(true) 
      bool(false)
    1. date 将时间戳转为指定时间格式的字符串
      echo date('Y-m-d Hs');
      echo '<br />';
      echo date('Y-m-d Hs', time());
      echo '<br />';
      echo date('Y-m-d Hs', strtotime('10minutes'));
      //输出结果
      2019-05-30 20:55:53
      2019-05-30 20:55:53
      2019-05-30 21:05:53
    1. strtotime 将指定字符串转为时间戳
      echo '前一秒:'.strtotime('-1seconds');
      echo '<br />';
      echo '当前时间:'.time();
      echo '<br />';
      echo '后一秒:'.strtotime('1seconds');
      echo '<br />';
      echo '一分钟后:'.strtotime('1minutes');
      echo '<br />';
      echo '一分钟前:'.strtotime('-1minutes');
      //输出结果
      前一秒:1559268932
      当前时间:1559268933
      后一秒:1559268934
      一分钟后:1559268993
      一分钟前:1559268873
    1. array_values 返回数组中所有的值,并给其重建数字索引
      $arr = ['color1' => 'red', 'color2' => 'green', 10, 20];
      print_r(array_values($arr));
      //输出结果
      Array ( [0] => red [1] => green [2] => 10 [3] => 20 )
    1. json_encode 对变量进行 json 编码
      $arr = array(10,20,30,40);
      $arr2 = array('1' => 10,20,30,40);
      $str1 = json_encode($arr);
      $str2 = json_encode($arr2);
      echo $str1.'<br />'.$str2;
      //输出结果
      [10,20,30,40]
      {"1":10,"2":20,"3":30,"4":40}
    1. json_decode 对 json 格式的字符串进行解码
      $str1 = '[10,20,30,40]';
      $str2 = '{"1":10,"2":20,"3":30,"4":40}';
      $obj = json_decode($str1);
      $res = json_decode($str2);
      $res2 = json_decode($str2,true);
      $res3 = json_decode($str2,false);
      print_r($obj);
      echo '<br />';
      print_r($res);
      echo '<br />';
      print_r($res2);
      echo '<br />';
      print_r($res3);
      //输出结果
      Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 ) 
      stdClass Object ( [1] => 10 [2] => 20 [3] => 30 [4] => 40 ) 
      Array ( [1] => 10 [2] => 20 [3] => 30 [4] => 40 ) 
      stdClass Object ( [1] => 10 [2] => 20 [3] => 30 [4] => 40 )
    1. range 创建指定范围的数组
      $arr = range(0,10);
      $arr2 = range(1,7);
      $arr3 = range(0,10,2);
      print_r($arr);
      echo '<br />';
      print_r($arr2);
      echo '<br />';
      print_r($arr3);
      //输出结果
      Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 ) 
      Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 ) 
      Array ( [0] => 0 [1] => 2 [2] => 4 [3] => 6 [4] => 8 [5] => 10 )
    1. array_push 向数组中添加一个或多个元素(尾部插入)
      $arr = range(0,8);
      $res = array_push($arr,11,12); //返回数组的长度
      print_r($arr);
      //输出结果
      Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 11 [10] => 12 )
    1. array_pop 删除数组中最后一个元素
      $arr = range(0,8);
      $res = array_pop($arr);  //返回数组的长度
      print_r($arr);
      //输出结果
      Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 )
    1. array_shift 删除数组头部第一个元素
      $arr = range(2,10);
      print_r($arr);
      echo '<br />';
      $res = array_shift($arr);   //返回被删除的元素
      $res2 = array_shift($arr);  //返回被删除的元素
      print_r($arr);
      echo '<br />';
      print_r($res);
      echo '<br />';
      print_r($res2);
      //输出结果
      Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 9 [8] => 10 ) 
      Array ( [0] => 4 [1] => 5 [2] => 6 [3] => 7 [4] => 8 [5] => 9 [6] => 10 ) 
      2
      3
    1. array_unshift 往数组中添加一个或多个元素(头部插入)
      $arr = range(0,8);
      print_r($arr);
      array_unshift($arr,20,30);
      echo '<br />';
      print_r($arr);
      //输出结果
      Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 ) 
      Array ( [0] => 20 [1] => 30 [2] => 0 [3] => 1 [4] => 2 [5] => 3 [6] => 4 [7] => 5 [8] => 6 [9] => 7 [10] => 8 )
    1. file_get_contents() 将文件读入到一个字符串
      $str = file_get_contents("http://ci.com/home/welcome/index");
      $str2 = file_get_contents("http://ci.com/home/welcome/index",null,null,2,20);
    1. file_put_contents() 将字符串写入到一个文件中
      $size = file_put_contents("static/data.php", $str);//返回写入文件的字符数
    1. addslashes($str) 将目标字符串中的含义的引号,反斜杠,null字符等进行转义,防止拼接 SQL 语句时出错
      $name = "jack'liming";
      echo addslashes($name);
    1. set_time_limit(int $seconds)
      设置脚本最大执行时间,当脚本执行时间大于最大执行时间而报错时,可在程序前加上该函数来设置最大执行时间,即可解决该问题。
      set_time_limit(123);//最大执行时间 120 秒
      set_time_limit(0);//最大执行时间无限制
    1. strstr()
      $str1 = 'this is a beatuiful girl';
      $res = strstr($str1, 'iss'); // false
      $res1 = strstr($str1, 'is'); // is is a beatuiful girl
      $res2 = strstr($str1, 'is', true); // th
    1. array_unique() 对数组中元素去重
      $input = array("a" => "green", "red", "b"=>"green", "blue", "red", 'apple', 10, '10');
      $ret = array_unique($input);
    1. array_multisort() 数组按指定字段排序

      $arr[] = ['name' => '小李', 'age' => 10, 'num' => 10];
      $arr[] = ['name' => '小明', 'age' => 15, 'num' => 20];
      $arr[] = ['name' => '王二', 'age' => 20, 'num' => 8];
      $arr[] = ['name' => '小王', 'age' => 21, 'num' => 8];
      $arr[] = ['name' => '小野', 'age' => 19, 'num' => 8];
      
      array_multisort(array_column($arr,'age'), SORT_DESC, $arr);
本作品采用《CC 协议》,转载必须注明作者和本文链接
sunshine
讨论数量: 1

28点错了哦

4年前 评论
卡尔西法 (楼主) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!