前端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当中是否有这个概念? 所谓匿名,就是没有名字. 匿名函数,也就是没有函数名的函数. 匿名函数的第一种用法,直接把赋数赋值给变量,调用变量即为调用函数. 匿名函数的写法比较灵活. ...
随机推荐
- 1.hive介绍及安装配置
1.Hive介绍 数据库OLTP 在线事务处理 数据仓库OLAP 在线分析处理 延迟高 类sql方式(HQL) 使用sql方式,用来读写,管理位于分布式存储系统上的大型数据集的数据仓库技术 hive是 ...
- PHP中的变量名,函数名,类名是区分大小写的吗
在PHP中,自定义的函数名,类名,以及内置的函数,关键字是不区分大小写的,比如: class,Class,CLASS,while,While,ECHO,echo,NULL,Null 都是一样的. 但是 ...
- iPhone上的CPU架构,核数以及运行内存
机型 CPU架构 CPU名 CPU位数 CPU核数 运行内存 iPhone 5 ARMv7s A6 32bit 双核 1G iPhone 5c ARMV7s A6 32bit 双核 1G iPhone ...
- 【探路者】团队Alpha周贡献分数分配结果
经本组成员商议,根据老师提供的分数,(每人携带10分进入团队,[探路者]团队7人,共计35分). 本周每位同学携带10分进入组内,7人共计70分.分数公布如下: 吴雨丹 15分 贾雅杰 12分 蔺依铭 ...
- Windows 10 系统下Python环境的搭建与配置
1. 在https://www.python.org/downloads/release/python-352/网站上下载python版本 2. 配置环境 右击计算机属性,然后选择高级系统设置 3. ...
- Swift-闭包使用及解决循环引用问题
Swift中闭包使用参考OC中block使用,基本一致 // 闭包类型 首先写(参数列表)->(返回值类型) func loadData(callBack : (jsonData:String) ...
- inux下mysql的root密码忘记解决方法
1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...
- centOS7设置静态ip后无法上网的解决,【亲可测】
最近在VMware虚拟机里玩Centos,装好后发现上不了网.经过一番艰辛的折腾,终于找到出解决问题的方法了.最终的效果是无论是ping内网IP还是ping外网ip,都能正常ping通.方法四步走: ...
- Mysql中关键词执行顺序
MySQL的语句执行顺序 MySQL的语句一共分为11步,最先执行的总是FROM操作,最后执行的是LIMIT操作.其中每一个操作都会产生一张虚拟的表,这个虚拟的表作为一个处理的输入,只是这些虚拟的表对 ...
- 大家好,请问在DELPHI中#13和#10是表示什么含义的?
#13: 表示"回车"#10: 表示"换行" ASCII码 Delphi字符 C程序 含义------- ---------- ----- ------ ...