前端PHP入门-024-字符串函数-API查看
数组、字符串和数据库是我们函数里面最、最、最常用的三类函数,数组和数据库我们现在还没有讲到,等讲到的时候我们再来和大家细说。
当然PHP的字符串函数也有很多。我们最常使用的两个系列的字符串:
- 单字节字符串处理函数
- 多字节字符串处理函数
- 字符串编码转换函数
我们来说说为什么要学这么多函数:
- 我们学的是中文,是双字节或者三字节的。老外的函数只能处理英文和数字这些单字节的字符串处理不鸟中文。达不到我们的功能需求
- 有的时候需要做不同字符编码间的转换,例如:把GBK的转为UTF-8
- 英文这些字符在电脑里又是必须要处理的
因此,我们要学三个类型的常用字符串函数。
- 多字节iconv_* 系列
- 多字节mb_* 系列
- 处理英文系列
给大家精挑细选了一些:
| 函数名 | 描述 | 实例 |
|---|---|---|
| trim() | 删除字符串两端的空格或其他预定义字符 | "str=""\r\nHelloWorld!\r\n"";echotrim(" role="presentation" style="position: relative;">str=""\r\nHelloWorld!\r\n"";echotrim(str=""\r\nHelloWorld!\r\n"";echotrim(str); |
| rtrim() | 删除字符串右边的空格或其他预定义字符 | "str=""HelloWorld!\n\n"";echortrim(" role="presentation" style="position: relative;">str=""HelloWorld!\n\n"";echortrim(str=""HelloWorld!\n\n"";echortrim(str);" |
| chop() | rtrim()的别名 | 同上 |
| ltrim() | 删除字符串左边的空格或其他预定义字符 | "str=""\r\nHelloWorld!"";echoltrim(" role="presentation" style="position: relative;">str=""\r\nHelloWorld!"";echoltrim(str=""\r\nHelloWorld!"";echoltrim(str);" |
| dirname() | 回路径中的目录部分(我们把它归在了字符串函数里了) | echo dirname("c:/testweb/home.php"); |
| str_pad() | 把字符串填充为指定的长度 | str="HelloWorld";echostrpad(" role="presentation" style="position: relative;">str="HelloWorld";echostrpad(str="HelloWorld";echostrpad(str,20,"".""); |
| str_repeat() | 重复使用指定字符串 | echo str_repeat(".",13); |
| str_split() | 把字符串分割到数组中 | print_r(str_split("Hello")); |
| strrev() | 反转字符串 | echo strrev("Hello World!"); |
| wordwrap() | 按照指定长度对字符串进行折行处理 | "str=""Anexampleonalongwordis:Supercalifragulistic"";echowordwrap(" role="presentation" style="position: relative;">str=""Anexampleonalongwordis:Supercalifragulistic"";echowordwrap(str=""Anexampleonalongwordis:Supercalifragulistic"";echowordwrap(str,15);" |
| str_shuffle() | 随机地打乱字符串中所有字符 | echo str_shuffle("Hello World"); |
| parse_str() | 将字符串解析成变量 | "parse_str(""id=23&name=John%20Adams"",myArray);printr(" role="presentation" style="position: relative;">myArray);printr(myArray);printr(myArray);" |
| number_format() | 通过千位分组来格式化数字 | "echo number_format(""1000000""); echo number_format(""1000000"",2); echo number_format(""1000000"",2,"","",""."");" |
| strtolower() | 字符串转为小写 | echo strtolower("Hello WORLD!"); |
| strtoupper() | 字符串转为大写 | echo strtoupper("Hello WORLD!"); |
| ucfirst() | 字符串首字母大写 | echo ucfirst("hello world"); |
| ucwords() | 字符串每个单词首字符转为大写 | echo ucwords("hello world"); |
| htmlentities() | 把字符转为HTML实体 | str = ""John & 'Adams'""; echo htmlentities(" role="presentation" style="position: relative;">str = ""John & 'Adams'""; echo htmlentities(str = ""John & 'Adams'""; echo htmlentities(str, ENT_COMPAT); |
| htmlspecialchars() | 预定义字符转html编码 | |
| nl2br() | \\n转义为标签 |
echo nl2br("One line.\nAnother line."); |
| strip_tags() | 剥去 HTML、XML 以及 PHP 的标签 | echo strip_tags("Hello world!"); |
| addcslashes() | 在指定的字符前添加反斜线转义字符串中字符 | str=""Hello,mynameisJohnAdams."echo" role="presentation" style="position: relative;">str=""Hello,mynameisJohnAdams."echostr=""Hello,mynameisJohnAdams."echostr; echo addcslashes($str,'m');" |
| stripcslashes() | 删除由addcslashes()添加的反斜线 | echo stripcslashes("Hello, \my na\me is Kai Ji\m."); |
| addslashes() | 指定预定义字符前添加反斜线 | str="Who′sJohnAdams?";echoaddslashes(" role="presentation" style="position: relative;">str="Who′sJohnAdams?";echoaddslashes(str="Who′sJohnAdams?";echoaddslashes(str); |
| stripslashes() | 删除由addslashes()添加的转义字符 | echo stripslashes("Who\'s John Adams?"); |
| quotemeta() | 在字符串中某些预定义的字符前添加反斜线 | str=""Helloworld.(canyouhearme?)"";echoquotemeta(" role="presentation" style="position: relative;">str=""Helloworld.(canyouhearme?)"";echoquotemeta(str=""Helloworld.(canyouhearme?)"";echoquotemeta(str); |
| chr() | 从指定的 ASCII 值返回字符 | echo chr(052); |
| ord() | 返回字符串第一个字符的 ASCII值 | echo ord("hello"); |
| strcasecmp() | 不区分大小写比较两字符串 | echo strcasecmp("Hello world!","HELLO WORLD!"); |
| strcmp() | 区分大小写比较两字符串 | |
| strncmp() | 比较字符串前n个字符,区分大小写 | |
| strncasecmp() | 比较字符串前n个字符,不区分大小写 | int strncasecmp ( string str1,string" role="presentation" style="position: relative;">str1,stringstr1,stringstr2 , int $len ) |
| strnatcmp() | 自然顺序法比较字符串长度,区分大小写 | int strnatcmp ( string str1,string" role="presentation" style="position: relative;">str1,stringstr1,stringstr2 ) |
| strnatcasecmp() | 自然顺序法比较字符串长度,不区分大小写 | int strnatcasecmp ( string str1,string" role="presentation" style="position: relative;">str1,stringstr1,stringstr2 ) |
| chunk_split() | 将字符串分成小块 | str chunk_split(str body[,int" role="presentation" style="position: relative;">body[,intbody[,intlen[,str $end]]) |
| strtok() | 切开字符串 | str strtok(str str,str" role="presentation" style="position: relative;">str,strstr,strtoken) |
| explode() | 使用一个字符串为标志分割另一个字符串 | array explode(str sep,str" role="presentation" style="position: relative;">sep,strsep,strstr[,int $limit]) |
| implode() | 同join,将数组值用预订字符连接成字符串 | string implode ( string glue,array" role="presentation" style="position: relative;">glue,arrayglue,arraypieces ) |
| substr() | 截取字符串 | string substr ( string string,int" role="presentation" style="position: relative;">string,intstring,intstart [, int $length ] ) |
| str_replace() | 字符串替换操作,区分大小写 | mix str_replace(mix search,,mix" role="presentation" style="position: relative;">search,,mixsearch,,mixreplace,mix subject[,int &" role="presentation" style="position: relative;">subject[,int &subject[,int &num]) |
| str_ireplace() | 字符串替换操作,不区分大小写 | mix str_ireplace ( mix search,mix" role="presentation" style="position: relative;">search,mixsearch,mixreplace , mix subject [, int &" role="presentation" style="position: relative;">subject [, int &subject [, int &count ] ) |
| substr_count() | 统计一个字符串,在另一个字符串中出现次数 | int substr_count ( string haystack,string" role="presentation" style="position: relative;">haystack,stringhaystack,stringneedle [, int offset=0[,int" role="presentation" style="position: relative;">offset=0[,intoffset=0[,intlength ]] ) |
| substr_replace() | 替换字符串中某串为另一个字符串 | mixed substr_replace ( mixed string,string" role="presentation" style="position: relative;">string,stringstring,stringreplacement , int start[,int" role="presentation" style="position: relative;">start[,intstart[,intlength ] ) |
| similar_text() | 返回两字符串相同字符的数量 | int similar_text(str str1,str" role="presentation" style="position: relative;">str1,strstr1,strstr2) |
| strchr() | 返回一个字符串在另一个字符串中开始位置到结束的字符串 | string strstr ( string str,string" role="presentation" style="position: relative;">str,stringstr,stringneedle , bool $before_needle ) |
| strrchr() | 返回一个字符串在另一个字符串中最后一次出现位置开始到末尾的字符串 | string strrchr ( string haystack,mixed" role="presentation" style="position: relative;">haystack,mixedhaystack,mixedneedle ) |
| stristr() | 返回一个字符串在另一个字符串中开始位置到结束的字符串,不区分大小写 | string stristr ( string haystack,mixed" role="presentation" style="position: relative;">haystack,mixedhaystack,mixedneedle [, bool $before_needle = false ] ) |
| strtr() | 转换字符串中的某些字符 | string strtr ( string str,string" role="presentation" style="position: relative;">str,stringstr,stringfrom , string $to ) |
| strpos() | 寻找字符串中某字符最先出现的位置 | int strpos ( string haystack,mixed" role="presentation" style="position: relative;">haystack,mixedhaystack,mixedneedle [, int $offset = 0 ] ) |
| stripos() | 寻找字符串中某字符最先出现的位置,不区分大小写 | int stripos ( string haystack,string" role="presentation" style="position: relative;">haystack,stringhaystack,stringneedle [, int $offset ] ) |
| strrpos() | 寻找某字符串中某字符最后出现的位置 | int strrpos ( string haystack,string" role="presentation" style="position: relative;">haystack,stringhaystack,stringneedle [, int $offset = 0 ] ) |
| strripos() | 寻找某字符串中某字符最后出现的位置,不区分大小写 | int strripos ( string haystack,string" role="presentation" style="position: relative;">haystack,stringhaystack,stringneedle [, int $offset ] ) |
| strspn() | 返回字符串中首次符合mask的子字符串长度 | int strspn ( string str1,string" role="presentation" style="position: relative;">str1,stringstr1,stringstr2 [, int start[,int" role="presentation" style="position: relative;">start[,intstart[,intlength ]] ) |
| strcspn() | 返回字符串中不符合mask的字符串的长度 | int strcspn ( string str1,string" role="presentation" style="position: relative;">str1,stringstr1,stringstr2 [, int start[,int" role="presentation" style="position: relative;">start[,intstart[,intlength ]] ) |
| str_word_count() | 统计字符串含有的单词数 | mix str_word_count(str $str,[]) |
| strlen() | 统计字符串长度 | int strlen(str $str) |
| count_chars() | 统计字符串中所有字母出现次数(0..255) | mixed count_chars ( string string[,int" role="presentation" style="position: relative;">string[,intstring[,intmode ] ) |
| md5() | 字符串md5编码 | str=""Hello"";echomd5(" role="presentation" style="position: relative;">str=""Hello"";echomd5(str=""Hello"";echomd5(str) |
| iconv | ||
| mb_substr | 获取字符串的部分 | string mb_substr ( string str,int" role="presentation" style="position: relative;">str,intstr,intstart [, int length=NULL[,string" role="presentation" style="position: relative;">length=NULL[,stringlength=NULL[,stringencoding = mb_internal_encoding() ]] ) |
| mb_http_output | 设置/获取 HTTP 输出字符编码 | mixed mb_http_output ([ string $encoding = mb_http_output() ] ) |
| mb_strlen | 获取字符串的长度 | mixed mb_strlen ( string str[,string" role="presentation" style="position: relative;">str[,stringstr[,stringencoding = mb_internal_encoding() ] ) |
| iconv | 字符串按要求的字符编码来转换 | string iconv ( string incharset,string" role="presentation" style="position: relative;">incharset,stringincharset,stringout_charset , string $str ) |
| iconv_substr | 截取字符串的部分 | |
| iconv_get_encoding | 获取 iconv 扩展的内部配置变量 | |
| mb_substr_count | 统计字符串出现的次数 | |
| mb_check_encoding | 检查字符串在指定的编码里是否有效 | |
| mb_strrpos | 查找字符串在一个字符串中最后出现的位置 | |
| mb_split | 使用正则表达式分割多字节字符串 | |
| parse_url | 解释URL成为一个数组 |
注:mb_* 和iconv_* 他们可以处理多字节字符,例如:中文。
前端PHP入门-024-字符串函数-API查看的更多相关文章
- 前端PHP入门-030-文件函数API
bool file_exists ( $指定文件名或者文件路径) 功能:文件是否存在. bool is_readable ( $指定文件名或者文件路径) 功能:文件是否可读 bool is_write ...
- 前端学PHP之字符串函数
× 目录 [1]特点 [2]输出 [3]空格[4]大小写[5]HTML[6]格式化[7]比较 前面的话 字符串的处理和分析在任何编程语言中都是一个重要的基础,往往是简单而重要的.信息的分类.解析.存储 ...
- 前端PHP入门-008-自定义函数
大家已经是有编程经验的WEB前端,那么对于函数已经很熟悉了,PHP当中定义函数跟你们学习的JavaScript一样 想想有哪些函数类型? 我们在实际开发过程当中需要有很多功能都需要反复使用到,而这些反 ...
- 前端PHP入门-019-内置函数之数学函数-很重要
查看帮助文档为主 函数名 描述 实例 输入 输出 abs() 求绝对值 $abs = abs(-4.2); //4.2 数字 绝对值数字 ceil() 进一法取整 echo ceil(9.999); ...
- 前端PHP入门-015-递归函数-飘过
提要: 如果感觉吃力,web前端的各位同学,可以弃之. 递归函数,递归只是一个名字,而递归函数的规定: 函数体内调用函数自己 . 我们来计算 10!=10*9*8*7*6*5*4*3*2*1 web前 ...
- 前端PHP入门-012-回调函数[慎入]
尽力而为,对于WEB前端和美工同学,比较难了!但是你们都学过JS的闭包等操作,那么这里也一定没有问题! 回调函数,可以配合匿名函数和变量函数实现更加优美.复杂的一种函数结构. 回调函数,就是在处理一个 ...
- 前端PHP入门-018-内置函数之文件包含函数
在实际开发中,常常需要把程序中的公用代码放到一个文件中,使用这些代码的文件只需要包含这个文件即可.这种方法有助于提高代码的重用性,给代码的编写与维护带来很大的便利. 在PHP中, 有require.r ...
- 前端PHP入门-010-内部函数
内部函数,是指在函数内部又声明了一个函数. 注意事项: 内部函数名,不能是已存在的函数名 假设在函数a里面定义了一个内部函数,不能调用两次函数a. <?php function foo() { ...
- 前端PHP入门-009-匿名函数
想想JavaScript当中是否有这个概念? 所谓匿名,就是没有名字. 匿名函数,也就是没有函数名的函数. 匿名函数的第一种用法,直接把赋数赋值给变量,调用变量即为调用函数. 匿名函数的写法比较灵活. ...
随机推荐
- 4.hive的外部表和内部表
1.外部表和内部表区别 创建表时:创建内部表时,会将数据移动到数据仓库指向的路径:若创建外部表,仅记录数据所在的路径, 不对数据的位置做任何改变. 删除表时:在删除表的时候,内部表的元数据和数据会被一 ...
- PIGCMS 关闭聊天机器人(小黄鸡)
无脑操作举例 1.找到 WeixinAction.class.php 文件,路径: 你的版本\PigCms\Lib\Action\Home 2.查询 function chat ,在 chat() 函 ...
- c# printDialog不显示问题
1.遇到问题:同样的代码,一个可以运行成功,另一个失败.百思不得其解情况下,监视下看每一个参数的属性是否一样,但参数太多,需要时间. 主要问题一般归结为:两个项目的属性编译设置不同,果然,一个x86正 ...
- 02-JAVA 初始化
构造器 概念:在创建对象时被自动调用的方法,构造器采用和类名一样的名称 创建对象时,会为其分配存储空间,并调用相应的构造器进行初始化.这就确保了在操作对象之前,这个对象已经被恰当的初始化了. 不接受仁 ...
- Beta版冲刺前准备
[团队概要] 团队项目名:小葵日记 团队名:日不落战队 队员及角色: 队员 角色 备注 安琪 前端工程师 队长 佳莹 前端工程师 智慧 后端工程师 章鹏 后端工程师 语恳 UI设计师 炜坤 前端工程师 ...
- Alpha 冲刺10
队名:日不落战队 安琪(队长) 今天完成的任务 整理项目. okhttp学习第四弹. 明天的计划 okhttp学习第五弹. 阶段反思. 睡觉. 还剩下的任务 个人信息数据get. 遇到的困难 困难:好 ...
- 第八次java笔记
- 查看sqlserver数据库的编码格式
查询语句:SELECT COLLATIONPROPERTY('Chinese_PRC_Stroke_CI_AI_KS_WS', 'CodePage'): 查询结果: 936 简体中文GBK 950 ...
- css3 flex属性flex-grow、flex-shrink、flex-basis学习笔记
最近在研究css3的flex.遇到的flex:1;这一块,很是很纠结,flex-grow.flex-shrink.flex-basis始终搞不清,最经搜集了大量的介绍,应该能算是明白了.网上大部分解释 ...
- J2EE十三种技术规范介绍
J2EE的十三个技术规范 J2EE体系结构 一.JDBC:Java Data Base Connectivity,数据库连接 我们大家对微软公司的ODBC数据库访问接口比较熟悉,而在Java中创建数据 ...