(PHP 4, PHP 5)
strstr — 查找字符串的首次出現
說明
string strstr ( string$haystack
, mixed $needle
[, bool $before_needle
= false
] )
返回 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() - 返回字符串的子串