首先我们来看一看strstr()函数的语法。
strstr ( string $haystack , mixed $needle ,bool $before_needle = false )
$haystack:输入字符串。
$needle:如果 $needle 不是一个字符串,那么它将被转化为整型并且作为字符的序号来使用。
$before_needle:若为 true,strstr() 将返回 $needle 在 $haystack 中的位置之前的部分。
返回值:返回字符串的一部分或者 false(如果未发现 needle)。
代码示例:
1.只有两个参数
<?php$email = '124455555@gamil.com';echo $email."<br>";$domain = strstr($email, '@');echo $domain; ?>
输出:124455555@gamil.com @gamil.com
2.第三个参数为true
<?php$email = '124455555@gamil.com';echo $email."<br>";$domain = strstr($email, '@');echo $domain; // 打印 @example.comecho "<br>";$user = strstr($email, '@', true); // 从 php 5.3.0 起echo $user; // 打印 name?>
输出:124455555@gamil.com @gamil.com 124455555
推荐:《2021年php面试题大汇总(收藏)》《php视频教程》
以上就是如何使用php中的strstr()函数的详细内容。
