前端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当中是否有这个概念? 所谓匿名,就是没有名字. 匿名函数,也就是没有函数名的函数. 匿名函数的第一种用法,直接把赋数赋值给变量,调用变量即为调用函数. 匿名函数的写法比较灵活. ...
随机推荐
- ie6下,莫名被复制出一段文字解决
在IE6下使用浮动可能会出现文字重复的情况. 在IE6下,浮动层之间有注释文字的话,之前那个浮动层的内容文字就有可能遭遇一个“隐形”的复制,但是代码里查看文字可并没有多出来. 看个例子: HTML & ...
- java面向对象的冒泡排序,选择排序和插入排序的比较
这三种排序有俩个过程: 1.比较俩个数据. 2.交换俩个数据或复制其中一项. 这三种排序的时间级别 冒泡排序:比较 (N-1)+(N-2)+...+2+1 = N*(N-1)/2=N2/2 交换 0 ...
- scrum立会报告+燃尽图(第三周第五次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2286 项目地址:https://coding.net/u/wuyy694 ...
- pat甲级1002
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- 线段树-hdu2795 Billboard(贴海报)
hdu2795 Billboard 题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子 思路:每次找到最大值的位子,然后减去L 线段树功能:query:区间求最大值的位子(直接 ...
- Python学习笔记(二)--变量和数据类型
python中的数据类型 python中什么是变量 python中定义字符串 raw字符串与Unicode字符串 python中的整数和浮点数 python中的bool类型 --- python中的数 ...
- lintcode-436-最大正方形
436-最大正方形 在一个二维01矩阵中找到全为1的最大正方形 样例 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 返回 4 标签 动态规划 爱彼迎 脸书 思路 使用 ...
- lintcode-423-有效的括号序列
423-有效的括号序列 给定一个字符串所表示的括号序列,包含以下字符: '(', ')', '{', '}', '[' and ']', 判定是否是有效的括号序列. 样例 括号必须依照 "( ...
- 在本地数据库目录或系统数据库目录中已经存在数据库别名""的解决办法
在创建数据库时遇到数据库别名已存在的问题时,可以: 1. 首先用 db2 list database directory 命令看在系统数据库目录(System Database Directory)中 ...
- 【第八周】【新蜂】新NABCD
由小组成员宫成荣撰写 一.小组项目申请时提交的NABCD: 痛点:普通的俄罗斯方块是不现实距离下一级有多远的,我们的游戏能显示距离下一等级游戏有多远.方便玩家体验. nabc: n:能满足大多数玩家的 ...