您当前的位置: 首页 >  彭世瑜 php

PHP:150个内置函数简单整合

彭世瑜 发布时间:2019-03-10 22:40:29 ,浏览量:3

共计整理150个函数

一、数学函数

11个

函数说明举例结果abs绝对值abs(45), abs(-45)45, 45ceil向上取整ceil(5.5)6floor向下取整floor(5.5)5fmod浮点数取余fmod(5, 3)2pown次方运算pow(2, 3)8round四舍五入round(4.55555, 2)4.56sqrt求平方根sqrt(9)3max最大值max(1, 2, 3), max(array(1, 2, 3))3, 3min最小值min(1, 2, 3), min(array(2, 3, 4))1, 1rand随机数rand(1, 4), mt_rand(1, 4)4, 1pi圆周率值pi()3.1415926535898 二、字符串函数

57个

1、字符串处理

13个

函数说明举例结果trim修剪两边trim("\thello\t")helloltrim修剪左边ltrim("\thello\t")hello\trtrim修剪右边rtrim("\thello\t")\thellostr_pad字符串填充str_pad(“hello”, 10, “*”)hello*****str_repeat重复字符串str_repeat(“hello”, 2)hellohellostrrev反转字符串strrev(“hello”)ollehstr_shuffle打乱字符串str_shuffle(“hello”)lheolnumber_format格式化数字number_format(100000.897, 2, “.”, “-”)100-000.90strtolower字符串转为小写strtolower(“Hello”)hellostrtoupper字符串转为大写strtoupper(“hello”)HELLOucfirst字符串首字母大写ucfirst(“hello world”)Hello worlducwords字符串所有首字符大写ucwords(“hello world”)Hello Worldwordwrap字符串折行wordwrap(“hello world”, 5)hello\nworld 2、字符串网页相关

10个

函数说明举例结果parse_str字符串解析成变量parse_str(“id=23&name=Tom”, $list)Array([id] => 23 [name] => Tom)htmlentities字符转为HTML实体htmlentities(“hello&”)hello&htmlspecialchars预定义字符转html编码htmlspecialchars(“hello&”)hello&nl2br\n转义为标签nl2br(“hello\nworld”)hello\nworldstrip_tags剥去 HTML、XML 以及 PHP 的标签strip_tags(“Hello world!”)Hello world!addcslashes指定字符前添加反斜线addcslashes(“Hello number”, “n”)Hello \numberstripcslashes删除反斜线stripcslashes(“Hello \nu\mber”)Hello \numberaddslashes预定义字符前添加反斜线addslashes(“Hello’ number”)Hello’ numberstripslashes删除预定义字符前的转义字符stripslashes(“Hello’ number”)Hello’ numberquotemeta预定义的字符前添加反斜线quotemeta(“Hello world.”)Hello world.rawurldecode解码URL编码的字符串rawurldecode(‘my%20name’)my namerawurlencode字符串进行URL编码rawurlencode(‘my name’)my%20name 3、字符串比较

8个

函数说明举例结果chrASCII 值返回字符chr(65)Aord字符串第一个字符的ASCII值ord(“hello”)104strcasecmp不区分大小写比较两字符串strcasecmp(“Hello”, “HELLO”)0strcmp区分大小写比较两字符串strcmp(“Hello”, “HELLO”)32strncmp比较字符串前n个字符,区分大小写strncmp(“Hello”, “HELLO”, 3)32strncasecmp比较字符串前n个字符,不区分大小写strncasecmp(“Hello”, “HELLO”, 3)0strnatcmp自然顺序法比较字符串长度,区分大小写(“Hello”, “HELLO”)1strnatcasecmp自然顺序法比较字符串长度, 不区分大小写strnatcasecmp(“Hello”, “HELLO”)0 4、字符串切割与拼接

6个

函数说明举例结果chunk_split将字符串分成小块chunk_split(“hello world”, 3)hel lo wor ldstrtok切开字符串strtok(“hello world”, " ")helloexplode字符串分割explode(" ", “hello world”)Array([0] => hello [1] => world)str_split字符串分割str_split(“hello”)Array([0] => h [1] => e…)implode将数组值连接成字符串implode("+", array(“hello”, “world”))hello+worldsubstr截取字符串substr(“hello”, 1, 3)ell 5、字符串查找替换

15个

函数说明举例结果str_replace字符串替换,区分大小写str_replace(“h”, “xx”, “hello”)xxellostr_ireplace字符串替换,不区分大小写str_ireplace(“h”, “xx”, “hello”)xxellosubstr_replace字符串替换substr_replace(“hello, hello”, “x”, 1, 6)hxhellosimilar_text两字符串相同字符的数量similar_text(“hello”, “Hello”)5strstr字符串开始位置到结束的字符串strstr(“HELLO hello”, “l”)llostrchrstrstr()的别名strchr(“HELLO hello”, “l”)llostristr字符串开始位置到结束的字符串,不区分大小写stristr(“HELLO, hello”, “l”)LLO, hellostrrchr字符串最后一次出现位置开始到末尾的字符串strrchr(“hello”, “l”)lostrtr转换字符串中的某些字符strtr(“hello”, “ll”, “xx”)hexxostrpos字符最先出现的位置strpos(“HELLO, hello”, “l”)9stripos字符最先出现的位置,不区分大小写stripos(“HELLO, hello”, “l”)2strrpos字符最后出现的位置strrpos(“hello,HELLO”, “l”)3strripos字符最后出现的位置,不区分大小写strripos(“hello, HELLO”, “l”)10strspn字符串中首次符合mask的长度strspn(“www.baidu.com”, “ad”)0strcspn字符串中不符合mask的长度strcspn(“www.baidu.com”, “ad”)5 6、字符串统计

4个

函数说明举例结果substr_count统计字符串出现次数substr_count(“hello”, “l”)2str_word_count统计含有的单词数str_word_count(“hello world”)2strlen统计字符串长度strlen(“hello world”)11count_chars统计字母次数count_chars(“hello”))Array([0] => 0 [1] => 0… [255] => 0) 7、字符串其他函数

1个

函数说明举例结果md5字符串md5编码md5(“hello”)5d41402abc4b2a76b9719d911017c592 三、数组函数

33个

1、数组创建

5个

函数说明举例结果array数组创建array(“Dog”, “Cat”)Array([0]=>Dog [1]=>Cat)array_combine两个数组zip生成一个数组array_combine(array(“a”), array(“Cat”))Array([a]=>Cat)range定范围的数组range(0, 4, 2)Array([0]=>0 [1]=>2 [2]=>4)compact创建数组,变量名为键,变量值为值$name= “Tom”; $age = “38”;compact(“name”, “age”)Array([name]=>Tom [age]=>38)array_fill填充值生成数组array_fill(2, 1, “Dog”)Array([1]=>Dog) 2、数组合并和拆分

5个

函数说明举例结果array_chunk重组数组块$a=array("a"=>"Cat", "b"=>"Dog"); array_chunk($a, 1)Array([0] => Array([0] => Cat) [1]=>Array([0]=>Dog))array_merge合并array_merge(array("a"=>"Dog"), array("b"=>"Cat"))Array([a]=>Dog [b]=>Cat)array_slice切片array_slice(array(0=>"Dog",1=>"Cat"), 1, 1)Array([0] => Dog)array_diff差集array_diff(array(0=>"Cat",1=>"Dog"), array(0=>"Dog"))Array([0]=>Cat)array_intersect交集array_intersect(array(0=>"Cat"), array(1=>"Dog"))Array([1]=>Cat [2]=>Dog) 3、数组查找替换

5个

函数说明举例结果array_search查找值,返回键,没有返回假array_search(“Dog”, array(“a”=>“Dog”))aarray_splice数组替代array(0=>“Dog”, 1=>“Cat”), 0, 1, array(0=>“Tiger”)array(0=>“Tiger”)array_sum求和array_sum(array(0=>5, 1=>10))15in_array查找值 ,区分大小写in_array(“Tom”, array(“Tom”, “Jack”))1array_key_exists查找键array_key_exists(“b”, array(“b”=>“Cat”))1 4、数组元素操作

10个

函数说明举例结果key指针当前指向的键名key(array(“a”=>“Dog”,“b”=>“Cat”))acurrent当前元素current(array(“a”=>“Dog”,“b”=>“Cat”))Dognext下一元素next(array(“a”=>“Dog”,“b”=>“Cat”))Catprev上一元素prev(array(“a”=>“Dog”,“b”=>“Cat”)) Dogend指向最后一个元素end(array(“a”=>“Dog”,“b”=>“Cat”))Catreset指向第一个元素reset(array(“a”=>“Dog”,“b”=>“Cat”))Doglist序列解包list( a , a, a,b)=array(“Dog”,“Cat”)$a=“Dog”; $b=“Cat”array_shift返回删除的第一个元素array_shift(array(“Dog”,“Cat”))Dogarray_unshift开头插入元素array_unshift(&array(),“Dog”)Array([0]=>“Dog”)array_push最后压入元素array_push(&array(), “Dog”)Array([0]=>“Dog”)array_pop弹出最后一个元素array_pop(&array(), “Dog”)Dogshuffle数组打乱,保留键名shuffle(array(“a”=> “Dog”))Array(“a”=> “Dog”)count计算个数count(array(“Tom”))1array_flip键值反转array_flip(array(“Tom”, “Jack”))Array([Tom]=>0 [Jack]=>1)array_keys返回所有键array_keys(array(“Tom”, “Jack”))Array([0]=>0 [1]=>1)array_values返回所有值array_values(array(“Tom”, “Jack”))Array([0]=>Tom [1]=>Jack)array_reverse元素顺序反转array_reverse(array(“Tom”, “Jack”))Array([0]=>Jack [1]=>Tom)array_count_values统计值次数array_count_values(array(“Tom”, “Jack”))Array([Tom]=>1 [Jack]=>1)array_rand随机取出键array_rand(array(“Tom”, “Jack”))1array_unique删除重复值,返回剩余数组array_unique(array(“Tom”, “Jack”))Array(“Tom”, “Jack”) 5、数组排序

8个

函数说明举例结果sort升序值排序,不保留键名sort(array(“Dog”=>“Tom”, “Cat”=>“Jack”))Array([0]=>Jack [1]=>Tom)rsort逆向排序,不保留键名rsort(array(“Dog”=>“Tom”, “Cat”=>“Jack”))Array([0]=>Tom [1]=>Jack)asort升序排序,保持索引关系asort(array(“Dog”=>“Tom”, “Cat”=>“Jack”))Array([Cat]=>Jack [Dog]=>Tom)arsort对数组逆向排序,保持索引关系arsort(array(“Dog”=>“Tom”, “Cat”=>“Jack”))Array([Dog]=>Tom [Cat]=>Jack)ksort键名排序ksort(array(“Dog”=>“Tom”, “Cat”=>“Jack”))Array([Cat]=>Jack [Dog]=>Tom)krsort逆向排序krsort(array(“Dog”=>“Tom”, “Cat”=>“Jack”))Array([Dog]=>Tom [Cat]=>Jack)natsort自然顺序排序natsort(array(“Dog”=>“Tom”, “Cat”=>“Jack”))Array([Cat]=>Jack [Dog]=>Tom)natcasesort自然排序,不区分大小写natcasesort(array(“Dog”=>“Tom”, “Cat”=>“Jack”))Array([Cat]=>Jack [Dog]=>Tom) 四、文件系统函数

39个

1、文件属性

9个

函数说明举例结果file_exists检查文件或目录是否存在file_exists(“demo.txt”)1filesize文件大小bytefilesize(“demo.txt”)18is_readable文件是否可读is_readable(“demo.txt”)1is_writable文件是否可写is_writable(“demo.txt”)1is_executable文件是否可执行is_executable(“demo.txt”)1filectime文件创建时间filectime(“demo.txt”)1552228956filemtime文件的修改时间filemtime(“demo.txt”)1552228956fileatime上次访问时间fileatime(“demo.txt”)1552228956stat文件属性stat(“demo.txt”)Array 2、文件操作

16个

函数说明举例结果fopen打开文件或者 URLfopen(“demo.txt”, “r”)$handlefclose关闭文件指针fclose($handle)true/falsefwrite写入文件fwrite($handle, $data)-fputs写入文件(同上)--feof文件指针是否到了文件结束的位置feof($handle)1fgets从文件指针中读取一行fgets($handle)$linefgetc从文件指针中读取字符fgetc($handle)$charfile读入整个文件到数组file(“demo.txt”)$linesreadfile读入一行readfile(“demo.txt”)$linefile_get_contents整个文件读入一个字符串file_get_contents(“demo.txt”)$contentfile_put_contents将字符串写入文件file_put_contents(“demo.txt”, $data)-ftell返回文件指针读/写的位置fseek在文件指针中定位rewind倒回文件指针的位置flock轻便的执行文件锁定fread读取字符fread($handle, 3)$chars 3、目录

12个

函数说明举例结果basename返回文件名basename(“root/demo.txt”)demo.txtdirname返回目录dirname(“root/demo.txt”)rootpathinfo文件信息pathinfo(“root/demo.txt”)Array([dirname] => root [basename] => demo.txt [extension] => txt [filename] => demo)opendir打开目录句柄opendir(’/’)$dir_handlereaddir从目录句柄中读取条目readdir($dir_handle)closedir关闭目录句柄closedir($dir_handle)rewinddir目录流重置到目录的开头rewinddir($dir_handle)mkdir新建目录rmdir删除目录unlink删除文件copy拷贝文件rename重命名一个文件或目录 4、文件的上传与下载

2个

函数说明举例结果is_uploaded_file判断文件是否是通过 HTTP POST上传的move_uploaded_file将上传的文件移动到新位置 五、时间

9个

函数说明举例结果time时间戳time()1552231653mktime取得时间戳mktime(0, 0, 0, 4, 25, 2012)1335283200date时间格式化date(‘Y年m月d日 H:i:s’)2019年03月10日 23:28:12checkdate验证一个格里高里日期date_default_timezone_set设定默认时区getdate取得日期/时间信息getdate()Arraystrtotime将任何英文文本的日期时间描述解析为 Unix 时间戳strtotime(“last Monday”)1551628800microtime返回当前 Unix 时间戳和微秒数microtime()0.64341300 1552232106sleep休息sleep(3)- 六、其他函数

3个

函数说明举例结果intval获取变量的整数值intval(“1111”, 2); intval(“20”)15 ; 20isset检测变量是否设置isset($hi)falseempty检查一个变量是否为空empty($hi)truejson_encode对变量进行JSON编码json_encode([‘name’=>‘Tom’]){“name”:“Tom”}json_decodeJSON解码返回对象json_decode(’{“name”:“Tom”}’))object(stdClass){[“name”]=>“Tom”}json_decodeJSON解码返回数组json_decode(’{“name”:“Tom”}’, true)array(1) {[“name”]=>“Tom”}

参考: Php 常用内置函数总结

关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 3浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.1062s