2024 年日本 PHP 研討會

strstr

(PHP 4, PHP 5, PHP 7, PHP 8)

strstr尋找字串首次出現的位置

說明

strstr(字串 $haystack, 字串 $needle, 布林值 $before_needle = false): 字串|false

haystack 字串中第一次出現 needle 的位置開始,包含 needle,回傳 haystack 的一部分。

注意事項:

此函式區分大小寫。若要執行不區分大小寫的搜尋,請使用 stristr()

注意事項:

如果只需要判斷 needle 是否存在於 haystack 中,建議使用速度更快且占用更少記憶體的 str_contains() 函式。

參數

haystack

輸入的字串。

needle

要搜尋的字串。

在 PHP 8.0.0 之前,如果 needle 不是字串,它會被轉換為整數,並作為字元的序數值。此行為自 PHP 7.3.0 起已被棄用,強烈建議不要依賴此行為。根據預期的行為,needle 應該被明確地轉換為字串,或者應該明確地呼叫 chr() 函式。

before_needle

如果為 truestrstr() 會回傳 haystack 中第一次出現 needle 之前的部分(不包含 needle)。

回傳值

回傳字串的片段,如果找不到 needle,則回傳 false

更新日誌

版本 說明
8.0.0 needle 現在接受空字串。
8.0.0 不再支援傳遞 整數 作為 needle
7.3.0 傳遞 整數 作為 needle 已被棄用。

範例

範例 #1 strstr() 範例

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

$user = strstr($email, '@', true);
echo
$user; // 顯示 name
?>

參見

  • stristr() - 不區分大小寫的 strstr
  • strrchr() - 尋找字串中最後一次出現的字元
  • strpos() - 尋找子字串在字串中第一次出現的位置
  • strpbrk() - 在字串中搜尋任何一組字元
  • preg_match() - 執行正規表示式比對

新增註解

使用者貢獻的註解 8 則註解

laszlo dot heredy at gmail dot com
11 年前
使用 strstr() 並不能避免 strpos() 的型別檢查。

如果 $needle 是 $haystack 中的最後一個字元,並且將 $needle 作為布林值本身測試會評估為 false,那麼將 strstr() 作為布林值測試也會評估為 false(因為如果成功,strstr() 會返回 $needle 的第一次出現以及 $haystack 的其餘部分)。

<?php
findZero
('01234'); // 找到零
findZero('43210'); // 沒找到零
findZero('0'); // 沒找到零
findZero('00'); // 找到零
findZero('000'); // 找到零
findZero('10'); // 沒找到零
findZero('100'); // 找到零

function findZero($numberString) {
if (
strstr($numberString, '0')) {
echo
'找到零';
} else {
echo
'沒找到零';
}
}
?>

此外,strstr() 比 strpos() 消耗更多記憶體,尤其是在 $haystack 是較長字串的情況下,所以如果您不關心 strstr() 返回的子字串,則不應該使用它。

PHP 沒有僅檢查 $needle 是否出現在 $haystack 中的函式;strpos() 會透過返回 false 告訴您它_沒有_出現,但如果它_有_出現,它會以整數形式告訴您它出現的_位置_,如果 $needle 是 $haystack 的第一部分,則為 0(零),這就是為什麼測試 if (strpos($needle, $haystack)===false) 是確定 $needle 不是 $haystack 的一部分的唯一方法。

我的建議是立即開始重視型別檢查,並熟悉您正在使用的函式的返回值。

致敬。
Gevorg Melkumyan
3 年前
別把這個函式跟 strtr 搞混了,我因此浪費了大概 1 個小時
Julian Egelstaff
1 年前
在舊程式碼中要注意邏輯反轉!

在 PHP 8 中,如果 needle 是空字串,這個函式會回傳 0(而不是 false),意味著字串的第一個字元符合 needle。在 PHP 8 之前,當 needle 是空字串時,它會回傳 false。

其他字串函式在 PHP 8 中也受到類似問題的影響:strpos()、strrpos()、stripos()、strripos()、strchr()、strrchr()、stristr(),以及這個函式 strstr()

如果您正在檢查返回值是否 === false,那麼您會被這個新行為誤導。您還需要檢查 needle 是否為空字串。基本上,像這樣:

<?php
$result
= $needle ? strstr($haystack, $needle) : false;
?>
xslidian at lidian dot info
11 年前
提供給需要搜尋字串最後一次出現位置的人

<?php
function strrstr($h, $n, $before = false) {
$rpos = strrpos($h, $n);
if(
$rpos === false) return false;
if(
$before == false) return substr($h, $rpos);
else return
substr($h, 0, $rpos);
}
?>
gruessle at gmail dot com
13 年前
已經使用這個好幾年了

<?php
/**
*
* @author : Dennis T Kaplan
*
* @version : 1.0
* Date : 2007 年 6 月 17 日
* Function : 反向 strstr()
* Purpose : 回傳 haystack 字串中,從開頭到第一次出現 needle 的部分
* $haystack = 'this/that/whatever';
* $result = rstrstr($haystack, '/')
* $result == this
*
* @access public
* @param string $haystack, string $needle
* @return string
**/

function rstrstr($haystack,$needle)
{
return
substr($haystack, 0,strpos($haystack, $needle));
}
?>

你可以把它改成
rstrstr ( string $haystack , mixed $needle [, int $start] )
<?php

函式 rstrstr($haystack,$needle, $start=0)
{
返回
substr($haystack, $start,strpos($haystack, $needle));
}

?>
w3b_monk3y at yahoo dot com
15 年前
如果您想在 5.3 以前的版本模擬 strstr 的 before_needle 參數,使用 strtok 比用 strpos 查找 needle 後再用 substr 切割更快。差異程度取決於字串大小,但 strtok 總是更快。
brett dot jr dot alton at gmail dot com
17 年前
若要在使用 PHP 5.x 或更低版本時使用 needle_before(第一次出現)參數,請嘗試

<?php
$haystack
= 'php-homepage-20071125.png';
$needle = '-';
$result = substr($haystack, 0, strpos($haystack, $needle)); // $result = php
?>
trent dot renshaw at objectst dot com dot au
9 年前
> root at mantoru dot de

PHP 讓這件事變得簡單。處理電子郵件地址的網域名稱部分時,只需將 strstr() 的返回值傳遞給 substr() 並從 1 開始即可。

substr(strstr($haystack, '@'), 1);
To Top