PHP 常用函式

卡爾西法發表於2019-05-22
    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

相關文章