欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP字符串函数 strstr()使用详解

程序员文章站 2022-07-09 19:46:38
定义 strstr 按分隔符截取字符串 语法 返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。包括needle。 不存在则返回false。 如果仅仅想确定 needle 是否存在于 haystack 中,可以使用速度更快、耗费内存更少的 str ......

定义

strstr - 按分隔符截取字符串

语法

strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) : string

返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。包括needle。
不存在则返回false。

如果仅仅想确定 needle 是否存在于 haystack 中,可以使用速度更快、耗费内存更少的 strpos() 函数。

before_needle 如果为true,则返回 needle 在 haystack 中的位置之前的部分,不包括needle。

示例

<?php
$email  = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // @example.com

$user = strstr($email, '@', true); // 从 php 5.3.0 起
echo $user; // name,不包含@
?>