php学习笔记 --- 关于字符串函数
2017-01-29 15:39
726 查看
[本文由 程序员炼金术账号 发布,2017年2月28日]
mixed strpos ( string $haystack , mixed $needle)
查找字符串首次出现的index位置
找不到index则返回false
因此判断要用===
查找字符串最后出现的index位置
mixed stripos( string $haystack , string $needle)
i(ignorecase)代表着忽略大小写的意思
mixed strripos( string $haystack , string $needle)
int strlen ( string $string )
获取字符串的长度
mixed mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )
获取中文字符串的长度
替换
mixed str_replace ( mixed $search , mixed $replace , mixed $subject)
子字符串替换
该函数返回一个字符串或者数组。该字符串或数组是将 subject 中全部的 search 都被 replace 替换之后的结果。
$search 可以指定一个字符串或是一个字符串数组,当指定数组时,数组内的所有被找到的元素都被替换
忽略大小写进行替换
string strtr ( string $str , array $replace_pairs )
同时替换多个字符串
$replace_pairs = array('search' => 'replace',....)
截取子字符串
string substr ( string $string , int $start [, int $length ] )
$start可以是负数,如当$start = -1时,则指向字符串结尾处向前数第1个位置。
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
strstr的别名是strchr
返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。
若为 $before_needle为TRUE,strstr() 将返回 needle 在 haystack 中的位置之前的部分。
string strrchr ( string $haystack , mixed $needle )
该函数返回 haystack 字符串中的一部分,这部分以 needle 的最后出现位置开始,直到 haystack 末尾。
分割
1.按长度分割,返回数组
array str_split ( string $string [, int $split_length = 1 ] )
2.按长度分割,返回字符串
string chunk_split ( string $body [, int $chunklen = 76 [, string $end = "\r\n" ]] )
它会在每 chunklen 个字符后边插入 end。
3.正则表达式分割
array split ( string $pattern , string $string [, int $limit ] )
如果设置了 limit 参数并且是正数,则返回的数组包含最多 limit 个元素,而最后那个元素将包含 string 的剩余部分。
如果 limit 参数是负数,则返回除了最后的 -limit 个元素外的所有元素。
4.单个字符串分割
array explode ( string $delimiter , string $string [, int $limit ] )
连接
string implode ( string $glue , array $pieces )
explode的逆向函数,用$glue将$pieces内的每个成员连接起来
字符串反转
string
strrev ( string
返回
中文字符串反转
mixed strpos ( string $haystack , mixed $needle)
查找字符串首次出现的index位置
找不到index则返回false
因此判断要用===
<?php $string = 'abc'; $search = 'a'; $pos = strpos($string,$search); if($pos === false){ echo "Not Found."; }else{ echo "Found."; } ?>mixed strrpos( string $haystack , mixed $needle)
查找字符串最后出现的index位置
mixed stripos( string $haystack , string $needle)
i(ignorecase)代表着忽略大小写的意思
mixed strripos( string $haystack , string $needle)
int strlen ( string $string )
获取字符串的长度
mixed mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )
获取中文字符串的长度
<?php $mystring = '中文'; echo strlen($mystring);//输出结果是6,utf8编码下一个汉字占3个字符 echo mb_strlen($mystring,'utf-8');//输出结果是2 ?>
替换
mixed str_replace ( mixed $search , mixed $replace , mixed $subject)
子字符串替换
该函数返回一个字符串或者数组。该字符串或数组是将 subject 中全部的 search 都被 replace 替换之后的结果。
$search 可以指定一个字符串或是一个字符串数组,当指定数组时,数组内的所有被找到的元素都被替换
//$search和$replace都是数组的情况 $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); $subject = 'A'; echo str_replace($search, $replace, $subject); // 输出 F ,因为 A 被 B 替换,B 又被 C 替换,以此类推...每次替换后又重头开始 // 由于从左到右依次替换,最终 E 被 F 替换mixed str_ireplace ( mixed $search , mixed $replace , mixed $subject)
忽略大小写进行替换
string strtr ( string $str , array $replace_pairs )
同时替换多个字符串
$replace_pairs = array('search' => 'replace',....)
截取子字符串
string substr ( string $string , int $start [, int $length ] )
$start可以是负数,如当$start = -1时,则指向字符串结尾处向前数第1个位置。
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
strstr的别名是strchr
返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。
若为 $before_needle为TRUE,strstr() 将返回 needle 在 haystack 中的位置之前的部分。
<?php $email = 'name@example.com'; $domain = strstr($email, '@'); echo $domain; // 打印 @example.com $user = strstr($email, '@', true); // 从 PHP 5.3.0 起 echo $user; // 打印 name ?>
string strrchr ( string $haystack , mixed $needle )
该函数返回 haystack 字符串中的一部分,这部分以 needle 的最后出现位置开始,直到 haystack 末尾。
分割
1.按长度分割,返回数组
array str_split ( string $string [, int $split_length = 1 ] )
2.按长度分割,返回字符串
string chunk_split ( string $body [, int $chunklen = 76 [, string $end = "\r\n" ]] )
它会在每 chunklen 个字符后边插入 end。
<?php $string = 'hihelloworld'; $result = chunk_split($string,2,'<br/>'); //分割后插入换行符 //hi<br/>he<br/>ll<br/>ow<br/>or<br/>ld<br/> echo $result; ?>
3.正则表达式分割
array split ( string $pattern , string $string [, int $limit ] )
如果设置了 limit 参数并且是正数,则返回的数组包含最多 limit 个元素,而最后那个元素将包含 string 的剩余部分。
如果 limit 参数是负数,则返回除了最后的 -limit 个元素外的所有元素。
<?php // 分隔符可以是斜线,冒号,或横线 $date = "04/30/1973-14:55:30"; list($month, $day, $year,$hour,$minute,$second) = split ('[/:-]', $date); echo "Month: $month; Day: $day; Year: $year; Hour: $hour; Minute: $minute; Second: $second;<br />\n"; ?>
4.单个字符串分割
array explode ( string $delimiter , string $string [, int $limit ] )
连接
string implode ( string $glue , array $pieces )
explode的逆向函数,用$glue将$pieces内的每个成员连接起来
字符串反转
string
strrev ( string
$string)
返回
string反转后的字符串。 只能反转英文
中文字符串反转
<?php $str = '苟利国家生死以,岂因祸福避趋之'; $result = ''; for($i = mb_strlen($str,'utf-8')-1; $i >= 0; $i--){ $result .= mb_substr($str, $i,1,'utf-8'); } echo $result;//之趋避福祸因岂,以死生家国利苟 ?>
相关文章推荐
- php学习笔记 -- 关于连接符与单双引号
- PHP学习笔记--php教科书上关于正则表达式实例代码(摘要)
- 关于php学习笔记二
- 关于php学习笔记五
- php学习笔记--关于时间戳
- php学习笔记(三)之发送邮件01 关于class_smtp.php
- 关于php学习笔记四
- php学习笔记(三)之发送邮件01 关于class_smtp.php
- 关于php基础学习笔记一
- PHP学习笔记11(字符串函数)
- 整理个关于PHP文件读写的学习笔记
- 关于php学习笔记三
- PHP7 学习笔记(三)关于PHP7如何安装调试工具Xdebug扩展以及Zephir的问题
- 关于JQuery 插件Grid的学习使用笔记 (PHP版)
- php学习笔记:NULL
- PHP and Web Services 学习笔记
- php学习笔记:可变变量、字符串运算符和数组运算符
- 摘抄笔记 关于学习
- PHP学习笔记(整理完成)
- PHP学习笔记(2)----Mysql与PHP的整合[原创]