(PHP 4, PHP 5, PHP 7, PHP 8)
strstr — 查找字符串的首次出現(xiàn)
返回 haystack 字符串從 needle 第一次出現(xiàn)的位置開始到 haystack 結(jié)尾的字符串。
注意:
該函數(shù)區(qū)分大小寫。如果想要不區(qū)分大小寫,請使用 stristr()。
注意:
如果你僅僅想確定
needle是否存在于haystack中,請使用速度更快、耗費(fèi)內(nèi)存更少的 strpos() 函數(shù)。
haystack輸入字符串。
needle
如果 needle 不是一個(gè)字符串,那么它將被轉(zhuǎn)化為整型并且作為字符的序號來使用。
before_needle
若為 true,strstr() 將返回 needle 在 haystack 中的位置之前的部分。
返回字符串的一部分或者 false(如果未發(fā)現(xiàn) needle)。
| 版本 | 說明 |
|---|---|
| 5.3.0 |
新增可選的 before_needle 參數(shù)。
|
| 4.3.0 | strstr() 成為二進(jìn)制安全的。 |
示例 #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
?>