PHP字符串函数之 strstr stristr strchr strrchr
- strstr -- 查找字符串的首次出现,返回字符串从第一次出现的位置开始到该字符串的结尾或开始。
- stristr -- strstr 函数的忽略大小写版本
- strchr -- strstr 函数的别名
- strrchr -- 查找字符串的最后一次出现,返回字符串从最后一次出现的位置开始到该字符串的结尾。
strstr
查找字符串的首次出现,返回字符串从第一次出现的位置开始到该字符串的结尾或开始。
mixed strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
参数说明
haystack
在该字符串中进行查找。
needle
如果 needle 不是一个字符串,那么它将被转换为整型并被视为字符的顺序值来使用。
before_needle
若为 TRUE,strstr() 将返回 needle 在 haystack 中的位置之前的部分。
返回值
成功:返回字符串 needle 之前或之后的一部分
失败:如果没找到 needle,将返回 FALSE。
注意
- 该函数区分大小写
- 如果你仅仅想确定 needle 是否存在于 haystack 中,请使用速度更快、耗费内存更少的 strpos() 函数
示例
<?php
/*【 needle 为单个字符 】 */
$email = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // 打印 @example.com
$user = strstr($email, '@', true); // 从 PHP 5.3.0 起
echo $user; // 打印 name
?>
<?php
/*【 needle 为数字 】 */
$email = 'name@example.com'; //字母a的 ASCII码为 97
$behind = strstr($email, 97);
echo $behind; // 打印 ame@example.com
$front = strstr($email, 97, true); // 从 PHP 5.3.0 起
echo $front; // 打印 n
?>
<?php
/*【 needle 为字符串 】 */
$email = 'name@example.com';
$behind = strstr($email, 'ex');
echo $behind; // 打印 example.com
$front = strstr($email, 'ex', true); // 从 PHP 5.3.0 起
echo $front; // 打印 name@
*/
?>
<?php
/*【 needle 为字符串 】 */
$email = 'name@example.com';
$behind = strstr($email, 'ab');
echo $behind; // 返回 false
$front = strstr($email, 'ab', true); // 从 PHP 5.3.0 起
echo $front; // 返回 false
*/
?>
stristr
strstr() 函数的忽略大小写版本
mixed stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
该函数与 strstr() 唯一的区别就是不区分大小写。其他可参考strstr()
<?php
$email = 'name@example.com';
$behind = stristr($email, 'A');
echo $behind; // 打印 ame@example.com
$front = stristr($email, 'A', true); // 从 PHP 5.3.0 起
echo $front; // 打印 n
?>
strchr
strstr() 函数的别名
mixed strchr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
该函数等同 strstr() 。其他可参考strstr()
$email = 'name@example.com';
$behind = strchr($email, 'a');
echo $behind; // 打印 ame@example.com
$front = strchr($email, 'a', true); // 从 PHP 5.3.0 起
echo $front; // 打印 n
?>
strrchr
查找字符串的最后一次出现,返回字符串从最后一次出现的位置开始到该字符串的结尾。
mixed strrchr ( string $haystack , mixed $needle )
参数说明
haystack
在该字符串中进行查找。
needle
如果 needle 包含了不止一个字符,那么仅使用第一个字符。该行为不同于 strstr()。
如果 needle 不是一个字符串,那么将被转化为整型并被视为字符顺序值。
返回值
成功:返回字符串 needle 之后的一部分
失败:如果没找到 needle,将返回 FALSE。
示例
<?php
/*【 needle 为字符 】 */
$email = 'name@example.com';
$behind = strrchr($email, 'a');
echo $behind; // 打印 ample.com
?>
/*【 needle 为字符串 】 */
$email = 'name@example.com';
$behind = strrchr($email, 'am');
echo $behind; // 打印 ample.com
?>
<?php
/*【 needle 为数字 】 */
$email = 'name@example.com';
$behind = strrchr($email, 97);
echo $behind; // 打印 ample.com
?>
OneAPM for PHP 能够深入到所有 PHP 应用内部完成应用性能管理 能够深入到所有 PHP 应用内部完成应用性能管理和监控,包括代码级别性能问题的可见性、性能瓶颈的快速识别与追溯、真实用户体验监控、服务器监控和端到端的应用性能管理。想阅读更多技术文章,请访问 OneAPM 官方技术博客。
本文转自 OneAPM 官方博客
PHP字符串函数之 strstr stristr strchr strrchr的更多相关文章
- strstr()查找函数,strchr(),strrchr(),stristr()/strpos(),strrpos()查找字符串位置
在一个较长的字符串这查找匹配的字符串或字符,其中strstr()和strchr()是完全一样的. 例: echo strstr('why always you','you'); 输出: you 如果为 ...
- php中strstr、strchr、strrchr、substr、stristr
一.strstr 和 strcchr的区别 strstr 显示第一次找到,要查找的字符串,以及后面的字符串. strrchr 显示最后一次找到,要查找的字符串,以及后面的字符串. 二.strstr ...
- c语言实现常见字符串函数strchr strstr strcmp atoi itoi(字符串换成整数)
好久没有更新博客了,刚刚出炉练练手的程序如下,很简单,没有考虑过多复杂的东西,有好的想法,欢迎指教: 字符查找函数: char *my_strchr(const char *s,int c) { ch ...
- 内存及字符串操作篇strlen strchar strcmp strcoll strcpy strdup strstr strtok strspn strrchr bcmp bcopy bzero index memccpy memset
bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...
- C语言-字符串函数的实现(五)之strstr
C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...
- 字符串函数(strcpy字符串拷,strcmp字符串比较,strstr字符串查找,strDelChar字符串删除字符,strrev字符串反序,memmove拷贝内存块,strlen字符串长度)
1.strcpy字符串拷贝拷贝pStrSource到pStrDest,并返回pStrDest地址(源和目标位置重叠情况除外) char *strcpy(char *pStrDest, const ch ...
- PHP字符串函数
php字符串处理函数大全 addcslashes — 为字符串里面的部分字符添加反斜线转义字符addslashes — 用指定的方式对字符串里面的字符进行转义bin2hex — 将二进制数据转换成十六 ...
- PHP部分字符串函数汇总
PHP部分字符串函数汇总 提交 我的评论 加载中 已评论 PHP部分字符串函数汇总 2015-03-10 PHP100中文网 PHP100中文网 PHP100中文网 微信号 功能介绍 互联网开发者社区 ...
- PHP函数积累总结(Math函数、字符串函数、数组函数)
Math函数:10个较常用标红.abs — 绝对值acos — 反余弦acosh — 反双曲余弦asin — 反正弦asinh — 反双曲正弦atan2 — 两个参数的反正切atan — 反正切ata ...
随机推荐
- 常用的HTML 标签二
<marquee></marquee> 滚动的文字,也称"走马灯" 语法格式 <marquee 属性="属性值">内容< ...
- CSS样式表与格式布局
样式表 CSS(Cascading Style Sheets 层叠样式表),作用是美化HTML网页. 内联样式表: 例:<p style="font-size:10px;" ...
- DNA RNA
核糖体没有DNA,但是有RNA(rRNA 核糖体RNA) 有DNA的细胞器是线粒体和叶绿体.
- 【风马一族_Android】Button 按钮之记录
Button button = new Button(); Button button = (Button)findViewById(R.id.bt_button);//让按钮显示灰色,失效 butt ...
- 《如何将windows上的软件包或文件上传到linux服务上》
昨天晚上朋友让我帮他简单的搭建个环境,他公司让他做款软件测试温度的,他自己搞的是嵌入式,在公司担任的是软件工程师,应届毕业生.也可能他们搞嵌入式的对这个linux系统不太熟,不会把windows上的软 ...
- background-size 设置背景图片的大小
background-size 设置背景图片的大小,以长度值或百分比显示,还可以通过cover和contain来对图片进行伸缩. 语法: background-size: auto | <长度值 ...
- NSS_06 extjs弹出窗口上的文本框默认获得焦点
这个问题其实是个窗户纸, 没什么技术含量,但是做的过程中有点曲折, 所以也记录下来吧. Ext.window.Window中有focus(o1, o2)方法, 作用:Try to focus this ...
- Oracle利用数据泵迁移用户
一.利用数据泵将数据导出 1.1.确定字符集: select * from v$nls_parameters; 或 select userenv('language') from dual; 1.2. ...
- EF-Code First 入门
本文程序基于VS2015.EF6.1,本文不做过多深入讨论,只是个入门. EF 就是微软的 EntityFramework,主要分为 DB First,Model First,Code First.之 ...
- XAML 概述三
通过对前面2节对XAML的介绍,我们对XAML有了一定的认识.这一节我们来简单了解一下部分XAML命名空间(x:)语言功能. x命名空间映射的是http://schemas.microsoft.com ...