文档首页> 常见问题> 180多个PHP常用函数总结

180多个PHP常用函数总结

发布时间:2023-05-04 04:30       

我们知道任何有效的 php 代码都有可能出现在函数内部,甚至包括其它函数和类定义。本文我们主要和大家分享180多个PHP常用函数总结,希望能帮助大家更高效的学习PHP。

数学函数

1.abs(): 求绝对值

$abs = abs(-4.2); //4.2
  • 1

  • 1

输入: 数字
输出: 绝对值数字

2.ceil(): 进一法取整

echo ceil(9.999); // 10
  • 1

  • 1

输出: 浮点数进一取整

3.floor(): 去尾法取整

    echo floor(9.999);  // 9
  • 1

  • 1

输出: 浮点数直接舍去小数部分

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
  • 1

  • 2

  • 3

  • 4

  • 1

  • 2

  • 3

  • 4

5.pow(): 返回数的n次方

    echo pow(-1, 20); // 1 基础数|n次方乘方值
  • 1

  • 1

6.round(): 浮点数四舍五入

    echo round(1.95583, 2); // 1.96, 一个数值|保留小数点后多少位,默认为0 舍入后的结果
  • 1

  • 1

7.sqrt(): 求平方根

    echo sqrt(9); //3 被开方的数平方根
  • 1

  • 1

8.max(): 求最大值

    echo max(1, 3, 5, 6, 7); // 7
  • 1

  • 1

多个数字或数组
返回其中的最大值

    echo max(array(2, 4, 5)); // 5
  • 1

  • 1

9.min(): 求最小值

输入: 多个数字或数组

输出: 返回其中的最小值

10.mt_rand(): 更好的随机数

输入: 最小|最大,
输出: 随机数随机返回范围内的值

    echo mt_rand(0,9);//n
  • 1

  • 1

11.rand(): 随机数
输入: 最小|最大,
输出: 随机数随机返回范围内的值

12.pi(): 获取圆周率值

字符串函数

去空格或或其他字符:

13.trim(): 删除字符串两端的空格或其他预定义字符

    $str = "\r\nHello World!\r\n";    echo trim($str);
  • 1

  • 2

  • 1

  • 2

输入: 目标字符串
返回值: 清除后的字符串

14.rtrim(): 删除字符串右边的空格或其他预定义字符

    $str = "Hello World!\n\n";    echo rtrim($str);
  • 1

  • 2

  • 1

  • 2

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

16.ltrim(): 删除字符串左边的空格或其他预定义字符

    $str = "\r\nHello World!";    echo ltrim($str);
  • 1

  • 2

  • 1

  • 2

17.dirname(): 返回路径中的目录部分

    echo dirname("c:/testweb/home.php");  //c:/testweb
  • 1

  • 1

输入: 一个包含路径的字符串
返回值: 返回文件路径的目录部分

字符串生成与转化:  

18.str_pad(): 把字符串填充为指定的长度

    $str = "Hello World";    echo str_pad($str,20,".");
  • 1

  • 2

  • 1

  • 2

输入:
要填充的字符串|新字符串的长度|供填充使用的字符串, 默认是空白

输出:
完成后的字符串

19.str_repeat(): 重复使用指定字符串

    echo str_repeat(".",13); // 要重复的字符串|字符串将被重复的次数13个点
  • 1

  • 1

20.str_split(): 把字符串分割到数组中

print_r(str_split("Hello"));
  • 1

  • 1

输入: 要分割的字符串|每个数组元素的长度,默认1

输出: 拆分后的字符串数组

21.strrev(): 反转字符串

    echo strrev("Hello World!"); // !dlroW olleH
  • 1

  • 1

输出: 目标字符串颠倒顺序后的字符串

22.wordwrap(): 按照指定长度对字符串进行折行处理

    $str = "An example on a long word is: Supercalifragulistic";    echo wordwrap($str,15);
  • 1

  • 2

  • 1

  • 2

输入: 目标字符串|最大宽数

输出: 折行后的新字符串

23.str_shuffle(): 随机地打乱字符串中所有字符

    echo str_shuffle("Hello World");
  • 1

  • 1

输入: 目标字符串顺序
输出: 打乱后的字符串

24.parse_str(): 将字符串解析成变量

    parse_str("id=23&name=John%20Adams", $myArray);
print_r($myArray);
  • 1

  • 2

  • 1

  • 2

输入: 要解析的字符串|存储变量的数组名称

输出:

Array(
[id] => 23[name] => John Adams)
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

25.number_format(): 通过千位分组来格式化数字
输入:
要格式化的数字|规定多少个小数|规定用作小数点的字符串|规定用作千位分隔符的字符串

输出:

1,000,000
1,000,000.00
1.000.000,00

大小写转换:

26.strtolower(): 字符串转为小写

    echo strtolower("Hello WORLD!");
  • 1

  • 1

目标字符串
小写字符串

27.strtoupper(): 字符串转为大写

    echo strtoupper("Hello WORLD!");
  • 1

  • 1

输出: 大写字符串

28.ucfirst(): 字符串首字母大写

    echo ucfirst("hello world"); // Hello world
  • 1

  • 1

29.ucwords(): 字符串每个单词首字符转为大写

    echo ucwords("hello world"); // Hello World
  • 1

  • 1

html标签关联:

30.htmlentities(): 把字符转为HTML实体

    $str = "John & 'Adams'";echo htmlentities($str, ENT_COMPAT); // John & 'Adams'
  • 1

  • 2

  • 1

  • 2

31.htmlspecialchars(): 预定义字符转html编码

32.nl2br(): \n转义为<br>标签

    echo nl2br("One line.\nAnother line.");
  • 1

  • 1

输出: 处理后的字符串

33.strip_tags(): 剥去 HTML、XML 以及 PHP 的标签

    echo strip_tags("Hello <b>world!</b>");  
  • 1

  • 1

34.addcslashes():在指定的字符前添加反斜线转义字符串中字符

    $str = "Hello, my name is John Adams.";    echo $str;    echo addcslashes($str,'m');
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

输入:
目标字符串|指定的特定字符或字符范围

35.stripcslashes(): 删除由addcslashes()添加的反斜线

    echo stripcslashes("Hello, \my na\me is Kai Ji\m.");    // 目标字符串 Hello, my name is Kai Jim.
  • 1

  • 2

  • 1

  • 2

36.addslashes(): 指定预定义字符前添加反斜线

    $str = "Who's John Adams?";echo addslashes($str);
  • 1

  • 2

  • 1

  • 2

输出: 把目标串中的’ ” \和null进行转义处理

37.stripslashes(): 删除由addslashes()添加的转义字符

    echo stripslashes("Who\'s John Adams?"); // 清除转义符号Who's John Adams?
  • 1

  • 1

38.quotemeta(): 在字符串中某些预定义的字符前添加反斜线

    $str = "Hello world. (can you hear me?)";echo quotemeta($str);    // Hello world\. \(can you hear me\?\)
  • 1

  • 2

  • 3

  • 1

  • 2

  • 3

39.chr(): 从指定的 ASCII 值返回字符

    echo chr(052); // ASCII 值返回对应的字符
  • 1

  • 1

40.ord(): 返回字符串第一个字符的ASCII值

    echo ord("hello"); 字符串第一个字符的 ASCII 值
  • 1

  • 1

字符串比较:

41.strcasecmp(): 不区分大小写比较两字符串

    echo strcasecmp("Hello world!","HELLO WORLD!");
  • 1

  • 1

输入:
两个目标字符串
输出:
大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);

数组函数

数组创建:

72.array(): 生成一个数组

    $a=array("Dog","Cat","Horse");
print_r($a);