php常用函數


數學函數

1.abs(): 求絕對值
$abs = abs(-4.2); //4.2 數字絕對值數字

2.ceil(): 進一法取整
echo ceil(9.999); // 10 浮點數進一取整

3.floor(): 舍去法取整
 echo floor(9.999); // 9 浮點數直接舍去小數部分

4.fmod(): 浮點數取余

 $x = 5.7;
 $y = 1.3; // 兩個浮點數,x>y 浮點余數
 $r = fmod($x, $y);
 // $r equals 0.5, because 4 * 1.3 + 0.5 = 5.7

5.pow(): 返回數的n次方
 echo pow(-1, 20); // 1 基礎數|n次方乘方值

6.round(): 浮點數四舍五入
 echo round(1.95583, 2); // 1.96, 一個數值|保留小數點后多少位,默認為0 舍入后的結果

7.sqrt(): 求平方根
 echo sqrt(9); //3 被開方的數平方根

8.max(): 求最大值
echo max(1, 3, 5, 6, 7); // 7
多個數字或數組 返回其中的最大值
echo max(array(2, 4, 5)); // 5

9.min(): 求最小值
輸入: 多個數字或數組
輸出: 返回其中的最小值

10.mt_rand(): 更好的隨機數
輸入: 最小|最大, 輸出: 隨機數隨機返回范圍內的值
echo mt_rand(0,9);//n


11.rand(): 隨機數 輸入: 最小|最大, 輸出: 隨機數隨機返回范圍內的值

12.pi(): 獲取圓周率值
去空格或或其他字符:

13.trim(): 刪除字符串兩端的空格或其他預定義字符
$str = "\r\nHello World!\r\n";
 echo trim($str);
輸入: 目標字符串 返回值: 清除后的字符串

14.rtrim(): 刪除字符串右邊的空格或其他預定義字符
 $str = "Hello World!\n\n";
 echo rtrim($str);

15.chop(): rtrim()的別名

16.ltrim(): 刪除字符串左邊的空格或其他預定義字符
 $str = "\r\nHello World!";
 echo ltrim($str);

17.dirname(): 返回路徑中的目錄部分
  echo dirname("c:/testweb/home.php");  //c:/testweb
輸入: 一個包含路徑的字符串 返回值: 返回文件路徑的目錄部分

字符串生成與轉化:  

18.str_pad(): 把字符串填充為指定的長度
$str = "Hello World";
 echo str_pad($str,20,".");
輸入: 要填充的字符串|新字符串的長度|供填充使用的字符串, 默認是空白
輸出: 完成后的字符串

19.str_repeat(): 重復使用指定字符串
echo str_repeat(".",13); // 要重復的字符串|字符串將被重復的次數13個點

20.str_split(): 把字符串分割到數組中
print_r(str_split("Hello"));
輸入: 要分割的字符串|每個數組元素的長度,默認1
輸出: 拆分后的字符串數組

21.strrev(): 反轉字符串
  echo strrev("Hello World!"); // !dlroW olleH
輸出: 目標字符串顛倒順序后的字符串

22.wordwrap(): 按照指定長度對字符串進行折行處理
$str = "An example on a long word is:
 Supercalifragulistic";
 echo wordwrap($str,15);
輸入: 目標字符串|最大寬數
輸出: 折行后的新字符串

23.str_shuffle(): 隨機地打亂字符串中所有字符
 echo str_shuffle("Hello World");
輸入: 目標字符串順序 輸出: 打亂后的字符串

24.parse_str(): 將字符串解析成變量
parse_str("id=23&name=John%20Adams", $myArray);
 print_r($myArray);
輸入: 要解析的字符串|存儲變量的數組名稱
輸出: 返回Array( [id] => 23 [name] => John Adams)

25.number_format(): 通過千位分組來格式化數字 輸入: 要格式化的數字|規定多少個小數|規定用作小數點的字符 串|規定用作千位分隔符的字符串
輸出: 1,000,000 1,000,000.00 1.000.000,00

大小寫轉換:

26.strtolower(): 字符串轉為小寫
 echo strtolower("Hello WORLD!");
目標字符串 小寫字符串

27.strtoupper(): 字符串轉為大寫
echo strtoupper("Hello WORLD!");
輸出: 大寫字符串

28.ucfirst(): 字符串首字母大寫
echo ucfirst("hello world"); // Hello world


29.ucwords(): 字符串每個單詞首字符轉為大寫
 echo ucwords("hello world"); // Hello World
html標簽關聯:

30.htmlentities(): 把字符轉為HTML實體
$str = "John & 'Adams'";
echo htmlentities($str, ENT_COMPAT); // John & 'Adams'

31.htmlspecialchars(): 預定義字符轉html編碼

32.nl2br(): \n轉義為<br>標簽
  echo nl2br("One line.\nAnother line.");
輸出: 處理后的字符串

33.strip_tags(): 剝去 HTML、XML 以及 PHP 的標簽
echo strip_tags("Hello <b>world!</b>");

 
34.addcslashes():在指定的字符前添加反斜線轉義字符串中字符
$str = "Hello, my name is John Adams.";
 echo $str;
 echo addcslashes($str,'m');
輸入: 目標字符串|指定的特定字符或字符范圍

35.stripcslashes(): 刪除由addcslashes()添加的反斜線
 echo stripcslashes("Hello, \my na\me is Kai Ji\m.");
    // 目標字符串 Hello, my name is Kai Jim.


36.addslashes(): 指定預定義字符前添加反斜線
 $str = "Who's John Adams?";
echo addslashes($str);
輸出: 把目標串中的' " \和null進行轉義處理

37.stripslashes(): 刪除由addslashes()添加的轉義字符
echo stripslashes("Who\'s John Adams?"); // 清除轉義符號Who's John Adams?


38.quotemeta(): 在字符串中某些預定義的字符前添加反斜線
$str = "Hello world. (can you hear me?)";
echo quotemeta($str);
 // Hello world\. \(can you hear me\?\)

39.chr(): 從指定的 ASCII 值返回字符
 echo chr(052); // ASCII 值返回對應的字符


40.ord(): 返回字符串第一個字符的ASCII值
 echo ord("hello"); 字符串第一個字符的 ASCII 值
字符串比較:

41.strcasecmp(): 不區分大小寫比較兩字符串
echo strcasecmp("Hello world!","HELLO WORLD!");
輸入: 兩個目標字符串 輸出: 大1|等0|小 -1

42.strcmp(): 區分大小寫比較兩字符串

43.strncmp(): 比較字符串前n個字符,區分大小寫
調用: int strncmp ( string $str1 , string $str2 , int $len)  

 44.strncasecmp(): 比較字符串前n個字符,不區分大小寫
調用: int strncasecmp ( string $str1 , string $str2 , int $len )

45.strnatcmp(): 自然順序法比較字符串長度,區分大小寫
調用: int strnatcmp ( string $str1 , string $str2 )
輸入: 目標字符串 

46.strnatcasecmp(): 自然順序法比較字符串長度, 不區分大小寫
調用: int strnatcasecmp ( string $str1 , string $str2 )
字符串切割與拼接:

47.chunk_split():將字符串分成小塊
調用: str chunk_split(str $body[,int $len[,str $end]])
輸入: $body目標字串, $len長度, $str插入結束符 輸出: 分割后的字符串

48.strtok(): 切開字符串
調用: str strtok(str $str,str $token)
目標字符串$str,以$token為標志切割返回切割后的字符串

49.explode(): 使用一個字符串為標志分割另一個字符串
調用: array explode(str $sep,str $str[,int $limit])
輸入: $sep為分割符,$str目標字符串,$limit返回數組最多包含元素數 輸出: 字符串被分割后形成的數組

50.implode(): 同join,將數組值用預訂字符連接成字符串
調用: string implode ( string $glue , array $pieces )
$glue默認, 用''則直接相連

51.substr(): 截取字符串
調用: string substr ( string $string , int $start [, int $length ] )


字符串查找替換
52.str_replace(): 字符串替換操作,區分大小寫
調用mix str_replace(mix $search,mix $replace, mix $subject[,int &$num])
輸入: $search查找的字符串,$replace替換的字符串,$subject被查找字串, &$num 輸出: 返回替換后的結果

53.str_ireplace() 字符串替換操作,不區分大小寫
調用: mix str_ireplace ( mix $search , mix $replace , mix $subject [, int &$count ] )
輸入: $search查找的字符串,$replace替換的字符串,$subject被查找字串,&$num 輸出: 返回替換后的結果

54.substr_count(): 統計一個字符串,在另一個字符串中出現次數
調用: int substr_count ( string $haystack , string $needle[, int $offset = 0 [, int $length ]] )

55.substr_replace(): 替換字符串中某串為另一個字符串
調用: mixed substr_replace ( mixed $string, string $replacement,int $start [, int $length ] )

56.similar_text(): 返回兩字符串相同字符的數量
調用: int similar_text(str $str1,str $str2)
輸入: 兩個比較的字符串
輸出: 整形,相同字符數量

57.strrchr(): 返回一個字符串在另一個字符串中最后一次出現位置開始到末尾的字符串
調用: string strrchr ( string $haystack , mixed $needle )

58.strstr(): 返回一個字符串在另一個字符串中開始位置到結束的字符串
調用: string strstr ( string $str, string $needle , bool $before_needle )   

 59.strchr(): strstr()的別名,返回一個字符串在另一個字符串中首次出現的位置開始到末尾的字符串
調用: string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )   

60.stristr(): 返回一個字符串在另一個字符串中開始位置到結束的字符串,不區分大小寫

調用:string stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] )

61.strtr(): 轉換字符串中的某些字符

調用: string strtr ( string $str , string $from , string $to )

62.strpos(): 尋找字符串中某字符最先出現的位置
調用: int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

63.stripos(): 尋找字符串中某字符最先出現的位置,不區分大小寫 調用: int stripos ( string $haystack , string $needle [, int $offset ] )

64.strrpos(): 尋找某字符串中某字符最后出現的位置
調用: int strrpos ( string $haystack , string $needle [, int $offset = 0 ] )

65.strripos(): 尋找某字符串中某字符最后出現的位置,不區分大小寫
調用: int strripos ( string $haystack , string $needle [, int $offset ] )

66.strspn(): 返回字符串中首次符合mask的子字符串長度 調用: int strspn ( string $str1 , string $str2 [, int $start [, int $length ]] )

67.strcspn(): 返回字符串中不符合mask的字符串的長度
調用: int strcspn ( string $str1 , string $str2 [, int $start [, int $length ]] )
輸入: $str1被查詢,$str2查詢字符串,$start開始查詢的字符,$length是查詢長度 輸出: 返回從開始到第幾個字符
字符串統計:

68.str_word_count(): 統計字符串含有的單詞數
調用: mix str_word_count(str $str,[])
輸入: 目標字符串 輸出: 統計處的數量

69.strlen(): 統計字符串長度int strlen(str $str)
輸入: 目標字符串 輸出:整型長度

70.count_chars(): 統計字符串中所有字母出現次數(0..255) 調用: mixed count_chars ( string $string [, int $mode ] )
字符串編碼:

71.md5(): 字符串md5編碼
$str = "Hello";
echo md5($str);



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM