定義和用法
strstr - 查找字串的首次出現版本支持
| PHP4 | PHP5 | PHP7 |
|---|---|---|
| 支持 | 支持 | 支持 |
V5.3.0 新增可選的 before_needle 引數,
V4.3.0 strstr() 成為二進制安全的,
語法
strstr (string $haystack , mixed $needle [, bool $before_needle = FALSE ] )
回傳 haystack 字串從 needle 第一次出現的位置開始到 haystack 結尾的字串,
該函式區分大小寫,如果想要不區分大小寫,請使用 stristr(),
如果你僅僅想確定 needle 是否存在于 haystack 中,請使用速度更快、耗費記憶體更少的 strpos() 函式,
引數
| 引數 | 必需的 | 描述 |
|---|---|---|
| haystack | 是 | 輸入字串, |
| needle | 是 | 如果 needle 不是一個字串,那么它將被轉化為整型并且作為字符的序號來使用, |
| before_needle | 否 | 若為 TRUE,strstr() 將回傳 needle 在 haystack 中的位置之前的部分, |
回傳值
回傳字串的一部分或者 FALSE(如果未發現 needle),$email = '[email protected]'; $domain = strstr($email, '@'); echo $domain; // 列印 @example.com echo "<br/>"; $user = strstr($email, '@', true); // 從 PHP 5.3.0 起 echo $user; // 列印 name
相關函式
preg_match() - 執行匹配正則運算式 stristr() - strstr 函式的忽略大小寫版本 strpos() - 查找字串首次出現的位置 strrchr() - 查找指定字符在字串中的最后一次出現 substr() - 回傳字串的子串轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/115613.html
標籤:PHP
上一篇:PHP 中四大經典排序演算法
