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 ...
随机推荐
- qt QLabel 显示网络图片
在网上试了很多代码都不能使用,自己写了写代码. 直接上代码 Codevoid QMusicLogo::setNetworkPic(const QString &szUrl) { QUrl ur ...
- multiple backgrounds 多重背景
语法缩写如下: background : [background-color] | [background-image] | [background-position][/background-siz ...
- PHP 如何判断当前用户已在别处登录
出处:http://bbs.lampbrother.net/read-htm-tid-121909-ds-1.html#tpc 主要思路:1.登录时,将用户的SessionID记录下来2.验证登录时, ...
- yum被锁定
使用Yum的时候 提示yum被搜定了 . Another app is currently holding the yum lock; waiting for it to exit... 解决办法 ...
- Response.Redirect和Server.Transfer
今天又比较闲,逛了逛园子,看看asp.net的内容,看到一篇关于这两个的比较: http://www.cnblogs.com/yunfeng8967/archive/2008/03/06/109323 ...
- python学习第三天第一部分
字典 1.字典的定义和规则: 定义:{key1:value1,key2:value2} key 的定义规则:1.必须是不可变的(数字.字符串.元组):2.必须是唯一的, value的定义规则:任意类型 ...
- openerp学习笔记 计算字段、关联字段(7.0中非计算字段、关联字段只读时无法修改保存的问题暂未解决)
计算字段.关联字段,对象修改时自动变更保存(当 store=True 时),当 store=False 时,默认不支持过滤和分组7.0中非计算字段.关联字段只读时无法修改保存的问题暂未解决 示例代码: ...
- 无法打开物理文件xxx.mdf 操作系统错误 5:“5(拒绝访问。)” (Microsoft SQL Server,错误: 5120) 的解决方法
问题描述:在附加数据库到sql server时,附加不上,出现如下图所示的错误 解决方法:找到xxx.mdf和xxx_log.ldf文件, 点击“右键”->“属性”->"安全&q ...
- zendframework 事件管理(二)
首先需要明确的几个问题: Q1.什么是事件? A:事件就是一个有名字的行为.当这个行为发生的时候,称这个事件被触发. Q2.监听器又是什么? A:监听器决定了事件的逻辑表达,由事件触发.监听器和事件往 ...
- linux下的汇编环境搭建(nasm)
第一步:先判断系统是否已经安装了nasm--------------->打开终端,执行whereis nasm :如果显示nasm: /usr/bin/nasm ,则已经安装:如果只显示nasm ...