Php/docs/function.strstr
来自菜鸟教程
strstr
(PHP 4, PHP 5, PHP 7)
strstr — 查找字符串的首次出现
说明
strstr
( string $haystack
, mixed $needle
[, bool $before_needle
= false
] ) : string
返回 haystack
字符串从 needle
第一次出现的位置开始到 haystack
结尾的字符串。
Note:
该函数区分大小写。如果想要不区分大小写,请使用 stristr()。
Note:
如果你仅仅想确定
needle
是否存在于haystack
中,请使用速度更快、耗费内存更少的 strpos() 函数。
参数
haystack
- 输入字符串。
needle
- 如果
needle
不是一个字符串,那么它将被转化为整型并且作为字符的序号来使用。 before_needle
- 若为
true
,strstr() 将返回needle
在haystack
中的位置之前的部分。
返回值
返回字符串的一部分或者 false
(如果未发现 needle
)。
更新日志
版本 | 说明 |
---|---|
5.3.0 | 新增可选的 before_needle 参数。
|
4.3.0 | strstr() 成为二进制安全的。 |
范例
Example #1 strstr() 范例
<?php$email = 'name@example.com';$domain = strstr($email, '@');echo $domain; // 打印 @example.com$user = strstr($email, '@', true); // 从 PHP 5.3.0 起echo $user; // 打印 name?>
参见
- preg_match() - 执行匹配正则表达式
- stristr() - strstr 函数的忽略大小写版本
- strpos() - 查找字符串首次出现的位置
- strrchr() - 查找指定字符在字符串中的最后一次出现
- substr() - 返回字符串的子串