【php精品源码栏目提醒】:网学会员在php精品源码频道为大家收集整理了“PHP中常用函数 - 其它资料“提供大家参考,希望对大家有所帮助!
PHP中常用函数数学函数ltphp//求绝对值mixedabsmixednumberabsabs‐3.2echoabsecholtbr/gt //3.2//进一取整intceilfloatnumber【ceilling】:天花板echoceil9.99echoltbr/gt//10//舍去法取整intfloorfloatnumberechofloor9.99echoltbr/gt //9//浮点数取余floatfmodfloatxfloatyx4.3y2.1rfmodxyechorecholtbr/gt //0.1//返回数的 N 次方numberpownumberbasenumberexpechopow213echoltbr/gt //8192//浮点数四舍五入floatroundfloatvalinrprecisionechoround1.324453echoltbr/gt //1.324//求平方根 floatsqrtfloatarg 【square】 :根,根源,祖先。
:正方形的,平方【root】echosqrt4echoltbr/gt //2//求最大值 mixedmaxnumberarg1numberarg2// mixedmaxarraynumbersarray...echomax12345echoltbr/gt //5echomaxarray356echoltbr/gt //6echomax0hello //0echoltbr/gt echovar_dumpmaxarray234array243 //array3 0gtint21gtint42gtint3echoltbr/gtechovar_dumpminstringarray24742 //string6quotstringquotecholtbr/gt//求最小值 mixedminnumberarg1numberarg2// mixedminarraynumbersarray...echomin12345 //1echoltbr/gtechominarray356 //3echoltbr/gtechomin0hello //0echoltbr/gtechovar_dumpminarray234array243//array30gtint21gtint32gtint4echoltbr/gtechovar_dumpminstringarray24742 //string6quotstringquotecholtbr/gt//随机取数intrand_intminintmaxechorand18echoltbr/gt//可替代 rand生成更好的随机数intmt_randintminintmaxechomt_rand19echoltbr/gt//获取圆周率 floatpivoidechopiecholtbr/gt //3.14159265359echoM_PI //3.14159265359M_PI是常量产生的值与 pi一样echoltbr/gt//任意进制之间的转换stringbase_convertstringnumberintfrombaseinttobasehexadecimalA23421echobase_converthexadecimal162echoltbr/gt //101000100011010000100001//计算指数floatexpfloatargechoexp0echoltbr/gt //1gt字符串函数ltphp/去空格或其他字符/ :修剪,整//trimstringstrstringcharlist删除字符串两端的空格或其他预定义字符【trim】理,削减 textquotttTheseareafewwords:...quot echotrimtext//quotTheseareafewwords:...quot echolthr/gt echotrimtextquottquot //quotTheseareafewwords:...quot echolthr/gt//rtrim删除字符串右边的空格或其他预定义字符其别名是 chop strquotHelloWorldnnquot echortrimstr//quotHelloWorldquot echolthr/gt echortrimstrquotnquot //quotHelloWorldquot echolthr/gt//ltrim删除字符串左边的空格或其他预定义字符 strquotnnHelloWorldquot echoltrimstr//quotHelloWorldquot echolthr/gt echoltrimstrquotnquot //quotHelloWorldquot echolthr/gt//dirnamestringpath返回路径中的目录部分 echodirnamequotc:/appserv/mysqlquot //c:/appserv echolthr/gt/ 字 符 串 生 成 与 转 化///str_padstringputintpad_lengthstringpad_stringintpad_type把字符串填充为指定 :填补的长度【pad】 strquotHelloWorldquot echostr_padstr20quot.quot//HelloWorld.........要填充的字符串新字符串的长度供填充使用的字符串,默认是空白 echolthr/gt//str_repeatstringinputintmultiplier重复使用指定字符串 echostr_repeatquot.quot13 //............. echolthr/gt//str_split把字符串分割到数组中【split】:分离,离开,劈开 print_rstr_splitquotHelloquot//Array0gtH1gte2gtl3gtl4gto echolthr/gt echostr_splitquotHelloquot //Array echolthr/gt//strrevstringstring反转字符串 【reverse】:相反;颠倒 echostrrevquotHelloWorldquot//dlroWolleH echolthr/gt//wordwrapstringstrintwidthstringbreakboolcut按照指定长度对字符串进行折行处理【wrap】:包,缠绕//分隔符break默认为n在源代码显示,若想看出效果需要加上ltbr/gt等cut判断是否分割单词,默认为 false,不分割。
strquotAnexampleonalongwordis:Supercalifragulisticquot echowordwrapstr15ltbr/gttrue //15 个字节后折行Anexampleona //目标字符串最大宽数 longwordis: //Supercalifragul //istic echolthr/gt//str_shuffle随机地打乱字符串中所有字符【shuffle】:洗牌,搅乱。
echostr_shufflequotHelloWorldquot echolthr/gt//parse_str将字符串解析成变量oidparse_strstringstrarrayamparr【parse】:解析,解释 parse_strquotid23ampnameJohn20AdamsquotmyArray print_rmyArray //Arrayidgt23namegtJohnAdams echolthr/gt//number_format要格式化的数字规定多少个小数规定用作小数点的字符串规定用作千位分隔符的字符串通过千位分组来格式化数字 echonumber_formatquot1000000quot //1000000 echolthr/gt echonumber_formatquot1000000quot2//1000000.00 echolthr/gt echonumber_formatquot1000000quot2quot.quotquotquot//1000000.00 echolthr/gt/ 大 小 写 转 化///strtolower字符串转为小写 echostrtolowerquotHelloWORLDquot//helloworld echolthr/gt//strtoupper字符串转为大写 echostrtoupperquothelloworldquot//HELLOWORLD echolthr/gt//ucfirst字符串首字母大写 【uc】:大写字体 echoucfirstquothelloworldquot//Helloworld echolthr/gt//ucwords字符串每个单词首字符转为大写 echoucwordsquothelloworldquot//HelloWorld echolthr/gt/ 字 符 串 比 较///strcasecmp不区分大小写比较两字符串 intstrcasecmpstringstr1stringstr2等于返回 0,gt0 返回正数,lt0 返回负数。
echostrcasecmpquotHelloworldquotquotHELLOWORLDquot //0 echolthr/gt echostrcasecmpquotHiworldquotquotHELLOWORLDquot //4 echolthr/gt//strcmp区分大小写比较两字符串 cmp:【compare】:比较 echostrcmpHelloworldHELLOWORLD//1 echolthr/gt//strncmp 比较字符串前 n 个字符区分大小写int strncmp string str1 stringstr2intlen echostrncmpHelloworldHELLOWORLD3//1 echolthr/gt//strncasecmp 比较字符串前 n 个字符不区分大小写intstrncasecmpstringstr1stringstr2intlen echostrncasecmpHelloworldHELLOWORLD3//0 echolthr/gt////strnatcmp 自然顺序法比较字符串长度区分大小写 echostrnatcmpquotHelloworldquotquotHELLOWORLDquot //1 echolthr/gt echostrnatcmpquotHiworldquotquotHELLOWORLDquot//1 echolthr/gt//strnatcasecmp自然顺序法比较字符串长度不区分大小写 intstrnatcasecmpstringstr1stringstr2 echostrnatcasecmpquotHelloworldquotquotHELLOWORLDquot//0 echolthr/gt echostrnatcasecmpquotHelloworldquotquotHELLOWORLDquot //0 echolthr/gt/ 字 符 串 切 割 与 拼 接///chunk_split 将字符串分成小块 str chunk_splitstr bodyint lenstr endbody 目标字串len 长度str 插入结束符【split】:分离,离开,劈开 strquotHelloworldquot newchunk_splitstr3 echonew //Helloworld echolthr/gt//strtok切开字符串 strstrtokstrstrstrtoken 目标字符串str,以token 为标志切割【token】:代表,记号,象征。
strquotHelloworldquot newstrtokstro echonew //Hell echolthr/gt//explode使用一个字符串为标志分割另一个字符串 【explode】;爆炸,激增。
//explodestrsepstrstrintlimit sep 为分割符str 目标字符串limit 返回数组最多包含元素数 strquotHelloworldquot newexplodelstr2 var_dumpnew echolthr/gt //array10gtstring12quotHelloworldquot//implode 同 join将数组值用预订字符连接成字符串 【implode】:向内破裂//stringimplodestringglue 提供一个连接字符或者字符串arraypieces 被连接的数组 glue 默认,用则直接相连 strarrayHelloworldHELLOWORLD echoimplodestr //HelloworldHELLOWORLD echolthr/gt//substr截取字符串 string substr string string int start int length 【sub】 :截取 echosubstrHelloworld23 echolthr/gt //llo/ 字 符 串 统 计///str_word_count统计字符串含有的单词数mixstr_word_countstrstr echostr_word_countHelloworld //2 echolthr/gt//strlen统计字符串长度 intstrlenstrstr echostrlenHelloworld //11 echolthr/gt//count_chars 统计字符串中所有字母出现次数0..255 mixedcount_charsstringstringintmode//可选参数mode默认值为0。
根据不同的mode,count_chars返回下列不同的结果://0‐以所有的每个字节值作为键名,出现次数作为值的数组。
//1‐与0相同,但只列出出现次数大于零的字节值。
//2‐与0相同,但只列出出现次数等于零的字节值。
//3‐返回由所有使用了的字节值组成的字符串。
//4‐返回由所有未使用的字节值组成的字符串。
arraycount_charsHelloworld1 var_dumparray //array8 32gt int172gtint1100gtint1101gtint1108gtint3111gtint2114gtint1119gtint1 echolthr/gt/字符串编码///md5字符串 md5 编码 strquotHelloquot echomd5str echolthr/gt //8b1a9953c4611296a827abf8c47804d7/Html 标 签 关 联///htmlentities 把字符转为 HTML 实体 strquotJohnampAdamsquot echohtmlentitiesstrENT_COMPAT //JohnampAdams源文件JohnampAdams echolthr/gt//htmlspecialchars 预定义字符转 html 编码 strquotJohnampAdamsquot echohtmlspecialcharsstrENT_QUOTES //John amp Adams 源 文 件 John ampamp039Adamsamp039 echoquotlthr/gtquot//nl2br n 转义为ltbrgt标签 echonl2brquotOneline.nAnotherline.quot //Oneline. echolthr/gt //Anotherline.//strip_tags剥去HTML、XML以及
PHP的标签 echostrip_tagsquotHelloltbgtworldlt/bgtquot //Helloworld echolthr/gt//addcslashes目标字符串指定的特定字符或字符范围 在指定的字符前添加反斜线转义字符串中字符 strquotHellomynameisJohnAdams.quot echostr //HellomynameisJohnAdams. echoaddcslashesstrm //HellomynameisJohnAdams. echolthr/gt//stripcslashes删除由 addcslashes添加的反斜线 echostripcslashesquotHellomynameisKaiJim.quot//HellomynameisKaiJim. echolthr/gt//addslashes指定预定义字符前添加反斜线把目标串中的quot和 null 进行转义处理 strquotWhosJohnAdamsquot echoaddslashesstr //WhosJohnAdams echolthr/gt//stripslashes 删除由 addslashes添加的转义字符 echostripslashesquotWhosJohnAdamsquot //WhosJohnAdams echolthr/gt//quotemeta 在字符串中某些预定义的字符前添加反斜线 strquotHelloworld.canyouhearmequot echoquotemetastr //Helloworld.canyouhearme echolthr/gt//hr 从指定的ASCII值返回字符 echochr052 // echolthr/gt//ord返回字符串第一个字符的ASCII值 echoordquotHelloquot //72 echolthr/gt/ 字 符 串 查 找 替 换///str_replace 字符串替换操作区分大小写 mixstr_replacemixsearchmixreplacemixsubjectintampnum//search 查找的字符串replace 替换的字符串subject 被查找字串ampnum strhellowOrld echostr_replaceoastr //hellawOrld echolthr/gt//str_ireplace 字符串替换操作不区分大小写search 查 找 的 字 符 串 replace 替 换 的 字 符串subject 被查找字串ampnum//mixstr_ireplacemixsearchmixreplacemixsubjectintampcount strhellowOrld echostr_ireplaceoastr //hellawarld echolthr/gt//substr_count 统计一个字符串在另一个字符串中出现次数//intsubstr_countstringhaystackstringneedleintoffset0intlength//string必需。
规定要检查的字符串。
replacement必需。
规定要插入的字符串。
//start可选。
规定在字符串中何处开始搜索。
charlist可选。
规定搜索的长度。
strhelloworld echosubstr_countstro35 //2 echolthr/gt//substr_replace替换字符串中某串为另一个字符串//mixedsubstr_replacemixedstringstringreplacementintstartintlength strhelloworld echosubstr_replacestraaa35 //helaaarld echolthr/gt//similar_text 返回两字符串相同字符的数量 intsimilar_textstrstr1strstr2 两 个比较的字符串 str1helloworld str2HelloWorld echosimilar_textstr1str2 //9 echolthr/gt//strrchr 返回一个字符串在另一个字符串中最后一次出现位置开始到末尾的字符串//stringstrrchrstringhaystackmixedneedle搜索字符串 strhelloworld echostrrchrstro //orld echolthr/gt//strstr返回一个字符串在另一个字符串中开始位置到结束的字符串//stringstrstrstringstr 被搜索的字符串stringneedle 搜索字符串boolbefore_needle strhelloworld echostrstrstro //oworld echolthr/gt//strchrstrstr的别名返回一个字符串在另一个字符串中首次出现的位置开始到末尾的字符串//stringstrstrstringhaystackmixedneedleboolbefore_needlefalse strhelloworld echostrchrstro //oworld echolthr/gt//stristr 返回一个字符串在另一个字符串中开始位置到结束的字符串,不区分大小写//stringstristrstringhaystackmixedneedleboolbefore_needlefalse strhelloworld echostristrstro //oworld echolthr/gt//strtr 转换字符串中的某些字符//stringstrtrstringstrstringfromstringto strhelloworld echostrtrstroa //hellawarld echolthr/gt//strpos寻找字符串中某字符最先出现的位置区分大小写//intstrposstringhaystackmixedneedleintoffset0 strhellOworld echostrposstro //7 echolthr/gt//stripos寻找字符串中某字符最先出现的位置不区分大小写//intstriposstringhaystackstringneedleintoffset strhellOworld echostriposstro //4 echolthr/gt//strrpos寻找某字符串中某字符最后出现的位置//intstrrposstringhaystackstringneedleintoffset0 strhelloworLd echostrrposstrl //3 echolthr/gt//strripos寻找某字符串中某字符最后出现的位置不区分大小写//intstrriposstringhaystackstringneedleintoffset strhelloworLd echostrriposstrl //9 echolthr/gt//strspn返回字符串中首次符合 mask 的子字符串长度//intstrspnstringstr1stringstr2intstartintlength strhelloworld echostrspnstrl37 //1 echolthr/gt//strcspn 返回字符串中不符合 mask 的字符串的长度str1 被查询,str2 查询字符串,start 开始查询的字符,length 查询长度//intstrcspnstringstr1stringstr2intstartintlength strhelloworld echostrcspnstrl47 //54 echolthr/gtgt数组函数ltphp//array生成一个数组aarraypigdogcatvar_dumpaecholthr/gt //array3 0gtstring3quotpigquot1gtstring3quotdogquot2gtstring3quotcatquot//array_combinearray1array2生成一个数组,用一个数组的值作为其键名,另一个数组的值作为其值【combine】:使联合,使结合a1arrayabca2arraypinkorangegreenprint_rarray_combinea1a2//Arrayagtpinkbgtorangecgtgreenjjecholthr/gt//range创建并返回一个包含指定范围的元素的数组(开始结束步长)numrange0505print_rnumecholthr/gt //Array0gt01gt52gt103gt154gt205gt256gt307gt358gt409gt4510gt50//compact()创建一个由参数所带变量组成的数组【compact】:合同,使紧凑,使简洁变量gt变量值firstnamequotPeterquotlastnamequotGriffinquotagequot38quotresultcompactquotfirstnamequotquotlastnamequotquotagequotprint_rresultecholthr/gt //ArrayfirstnamegtPeterlastnamegtGriffinagegt38//array_fill键填充数量填充内容用给定的值生成数组2 是键3 是填充的数量Dog为填充内容aarray_fill23quotDogquotprint_raecholthr/gt //Array2gtDog3gtDog4gtDog/数组的合并和拆分///array_chunkarraysizepreserve_key把一个数组分割成新的数组块【chunk:大块的;//true‐保留原始数组中的键名。
false‐默认。
每个结果数组使用从零开始的新数组索引。
aarrayquotCatquotquotDogquotquotHorsequotquotCowquotprint_rarray_.
上一篇:
程序模板说明
下一篇:
物流管理基础课程复习资料