ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
数组、字符串和数据库是我们函数里面最、最、最常用的三类函数,数组和数据库我们现在还没有讲到,等讲到的时候我们再来和大家细说。 当然PHP的字符串函数也有很多。我们最常使用的两个系列的字符串: 1. 单字节字符串处理函数 2. 多字节字符串处理函数 3. 字符串编码转换函数 我们来说说为什么要学这么多函数: 1. 我们学的是中文,是双字节或者三字节的。老外的函数只能处理英文和数字这些单字节的字符串处理不鸟中文。达不到我们的功能需求 2. 有的时候需要做不同字符编码间的转换,例如:把GBK的转为UTF-8 3. 英文这些字符在电脑里又是必须要处理的 因此,我们要学三个类型的常用字符串函数。 我们来贴个PHP手册的链接给大家看看: 1. 多字节iconv\_\* 系列:[http://phpbook.phpxy.com/book.iconv.html](http://phpbook.phpxy.com/book.iconv.html) 2. 多字节mb\_\* 系列:[http://php.net/manual/zh/book.mbstring.php](http://php.net/manual/zh/book.mbstring.php) 3. 处理英文系列:[http://phpbook.phpxy.com/book.strings.html](http://phpbook.phpxy.com/book.strings.html) 看了这三个链接是不是又被吓着了? ![document/2015-08-18/55d341cc9f228](http://box.kancloud.cn/document_2015-08-18_55d341cc9f228.png) 当然,你不用学这么多。PHP学院已经帮你把最常用的,需要强制背诵的准备好了。 ![document/2015-08-18/55d34215d28bb](http://box.kancloud.cn/document_2015-08-18_55d34215d28bb.png) 之前都教过大家用,那你开始背函数吧。 PHP常用函数: | 函数名 | 描述 | 实例 | | --- | --- | --- | | trim() | 删除字符串两端的空格或其他预定义字符 | "$str = ""\\r\\nHello World!\\r\\n""; echo trim($str); | | rtrim() | 删除字符串右边的空格或其他预定义字符 | "$str = ""Hello World!\\n\\n""; echo rtrim($str);" | | chop() | rtrim()的别名 | 同上 | | ltrim() | 删除字符串左边的空格或其他预定义字符 | "$str = ""\\r\\nHello World!""; echo ltrim($str);" | | dirname() | 回路径中的目录部分(我们把它归在了字符串函数里了) | echo dirname("c:/testweb/home.php"); | | str\_pad() | 把字符串填充为指定的长度 | $str = "Hello World"; echo str\_pad($str,20,"".""); | | str\_repeat() | 重复使用指定字符串 | echo str\_repeat(".",13); | | str\_split() | 把字符串分割到数组中 | print\_r(str\_split("Hello")); | | strrev() | 反转字符串 | echo strrev("Hello World!"); | | wordwrap() | 按照指定长度对字符串进行折行处理 | "$str = ""An example on a long word is: Supercalifragulistic""; echo wordwrap($str,15);" | | str\_shuffle() | 随机地打乱字符串中所有字符 | echo str\_shuffle("Hello World"); | | parse\_str() | 将字符串解析成变量 | "parse\_str(""id=23&name=John%20Adams"",$myArray); print\_r($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($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, my name is John Adams." echo $str; echo addcslashes($str,'m');" | | stripcslashes() | 删除由addcslashes()添加的反斜线 | echo stripcslashes("Hello, \\my na\\me is Kai Ji\\m."); | | addslashes() | 指定预定义字符前添加反斜线 | $str = "Who's John Adams?";echo addslashes($str); | | stripslashes() | 删除由addslashes()添加的转义字符 | echo stripslashes("Who's John Adams?"); | | quotemeta() | 在字符串中某些预定义的字符前添加反斜线 | $str = ""Hello world. (can you hear me?)""; echo quotemeta($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 $str2 , int $len ) | | strnatcmp() | 自然顺序法比较字符串长度,区分大小写 | int strnatcmp ( string $str1 , string $str2 ) | | strnatcasecmp() | 自然顺序法比较字符串长度,不区分大小写 | int strnatcasecmp ( string $str1 , string $str2 ) | | chunk\_split() | 将字符串分成小块 | str chunk\_split(str $body\[,int $len\[,str $end\]\]) | | strtok() | 切开字符串 | str strtok(str $str,str $token) | | explode() | 使用一个字符串为标志分割另一个字符串 | array explode(str $sep,str $str\[,int $limit\]) | | implode() | 同join,将数组值用预订字符连接成字符串 | string implode ( string $glue , array $pieces ) | | substr() | 截取字符串 | string substr ( string $string , int $start \[, int $length \] ) | | str\_replace() | 字符串替换操作,区分大小写 | mix str\_replace(mix $search,,mix $replace,mix $subject\[,int &$num\]) | | str\_ireplace() | 字符串替换操作,不区分大小写 | mix str\_ireplace ( mix $search , mix $replace , mix $subject \[, int &$count \] ) | | substr\_count() | 统计一个字符串,在另一个字符串中出现次数 | int substr\_count ( string $haystack , string $needle \[, int $offset = 0 \[, int $length \]\] ) | | substr\_replace() | 替换字符串中某串为另一个字符串 | mixed substr\_replace ( mixed $string , string $replacement , int $start \[, int $length \] ) | | similar\_text() | 返回两字符串相同字符的数量 | int similar\_text(str $str1,str $str2) | | strchr() | 返回一个字符串在另一个字符串中开始位置到结束的字符串 | string strstr ( string $str, string $needle , bool $before\_needle ) | | strrchr() | 返回一个字符串在另一个字符串中最后一次出现位置开始到末尾的字符串 | string strrchr ( string $haystack , mixed $needle ) | | stristr() | 返回一个字符串在另一个字符串中开始位置到结束的字符串,不区分大小写 | string stristr ( string $haystack , mixed $needle \[, bool $before\_needle = false \] ) | | strtr() | 转换字符串中的某些字符 | string strtr ( string $str , string $from , string $to ) | | strpos() | 寻找字符串中某字符最先出现的位置 | int strpos ( string $haystack , mixed $needle \[, int $offset = 0 \] ) | | stripos() | 寻找字符串中某字符最先出现的位置,不区分大小写 | int stripos ( string $haystack , string $needle \[, int $offset \] ) | | strrpos() | 寻找某字符串中某字符最后出现的位置 | int strrpos ( string $haystack , string $needle \[, int $offset = 0 \] ) | | strripos() | 寻找某字符串中某字符最后出现的位置,不区分大小写 | int strripos ( string $haystack , string $needle \[, int $offset \] ) | | strspn() | 返回字符串中首次符合mask的子字符串长度 | int strspn ( string $str1 , string $str2 \[, int $start \[, int $length \]\] ) | | strcspn() | 返回字符串中不符合mask的字符串的长度 | int strcspn ( string $str1 , string $str2 \[, int $start \[, int $length \]\] ) | | str\_word\_count() | 统计字符串含有的单词数 | mix str\_word\_count(str $str,\[\]) | | strlen() | 统计字符串长度 | int strlen(str $str) | | count\_chars() | 统计字符串中所有字母出现次数(0..255) | mixed count\_chars ( string $string \[, int $mode \] ) | | md5() | 字符串md5编码 | $str = ""Hello""; echo md5($str) | | iconv | | | | mb\_substr | 获取字符串的部分 | string mb\_substr ( string $str , int $start \[, int $length = NULL \[, string $encoding = mb\_internal\_encoding() \]\] ) | | mb\_http\_output | 设置/获取 HTTP 输出字符编码 | mixed mb\_http\_output (\[ string $encoding = mb\_http\_output() \] ) | | mb\_strlen | 获取字符串的长度 | mixed mb\_strlen ( string $str \[, string $encoding = mb\_internal\_encoding() \] ) | | iconv | 字符串按要求的字符编码来转换 | string iconv ( string $in\_charset , string $out\_charset , string $str ) | | iconv\_substr | 截取字符串的部分 | | | iconv\_get\_encoding | 获取 iconv 扩展的内部配置变量 | | | mb\_substr\_count | 统计字符串出现的次数 | | | mb\_check\_encoding | 检查字符串在指定的编码里是否有效 | | | mb\_strrpos | 查找字符串在一个字符串中最后出现的位置 | | | mb\_split | 使用正则表达式分割多字节字符串 | | | parse\_url | 解释URL成为一个数组 | | 注:mb\_\* 和iconv\_\* 他们可以处理多字节字符,例如:中文。 * * * 中文主要用的是GBK和utf-8两种编码格式。 GBK和utf-8是两个不同的编码委员会对于汉字进行的编码的标准。 他们规定GBK是双字节,也就是一个汉字占用2Bytes。 utf-8是三字节,一个汉字占用三个字节长度的存储空间。