前端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当中是否有这个概念? 所谓匿名,就是没有名字. 匿名函数,也就是没有函数名的函数. 匿名函数的第一种用法,直接把赋数赋值给变量,调用变量即为调用函数. 匿名函数的写法比较灵活. ...
随机推荐
- centos7.2 apache开启.htaccess
打开httpd.conf(在那里? APACHE目录的CONF目录里面),用文本编纂器打开后,查找 (1) AllowOverride None 改为 AllowOverride All (2)去掉下 ...
- pygame (1) 移动小乌龟
小乌龟图片素材: 第一个简单的小游戏: 小乌龟会不断的移动,并且每当到达窗口的左右边界的时候,还会自动的掉头. 源码: import pygame import sys# 导入sys模块,退出时使用 ...
- Scrum立会报告+燃尽图(十月二十五日总第十六次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...
- "Hello World"团队召开的第三周第七次会议
今天是我们团队“Hello World!”团队召开的第三周的第七次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时 ...
- 博弈---巴什博奕(Bash Game)(博弈入门)
巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规 定每次至少取一个,最多取m个.最后取光者得胜. 显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先取者拿走 ...
- Mysql中``和‘’的区别
两者在linux下和windows下不同,linux下不区分,windows下区分. 在windows下主要区别就是 单引号( ' )或双引号主要用于 字符串的引用符号 如: mysql> SE ...
- 初识ES6 解构
1.数组的解构 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构 例子: let [a, b, c] = [1, 2, 3]; console.log(a);//1cons ...
- WebService(一)
1.简介 Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用 ...
- kafka describe 显示结果解释
> bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic Topic:my- ...
- mysql流程控制语句
mysql编程 mysql编程中语句块包含符: 其实就是相当于js或php中大括号语法: [标识符:]begin //语句.... end [标识符]: 标识符就是定义定义的任意的名字而已,比如: ...