函式名:mb_strstr()
適用版本:PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8
用法:mb_strstr(string $haystack, string $needle, bool $before_needle = false, string $encoding = null): string|false
說明: mb_strstr() 函式用於在字串 $haystack 中查詢子字串 $needle,並返回 $needle 後的部分字串。該函式與原生的 strstr() 函式相似,但是支援多位元組字符集。
引數:
- $haystack:要在其中查詢子字串的原始字串。
- $needle:要查詢的子字串。
- $before_needle(可選):如果設定為 true,則返回 $needle 之前的部分字串。
- $encoding(可選):指定字元編碼。如果未指定,則使用內部字元編碼。
返回值:
- 如果找到了 $needle,則返回 $needle 後的部分字串。
- 如果未找到 $needle,則返回 false。
示例:
$str = "Hello, World!";
$needle = "World";
// 在 $str 中查詢 $needle 並返回 $needle 後的部分字串
$result = mb_strstr($str, $needle);
echo $result; // 輸出: World!
// 在 $str 中查詢 $needle 並返回 $needle 之前的部分字串
$result = mb_strstr($str, $needle, true);
echo $result; // 輸出: Hello,
// 使用指定的字元編碼進行查詢
$str = "你好,世界!";
$needle = "世界";
$result = mb_strstr($str, $needle, false, 'UTF-8');
echo $result; // 輸出: 世界!
注意事項:
- 如果 $encoding 引數未指定,則預設使用內部字元編碼。建議在使用前設定正確的字元編碼。
- 如果要查詢的子字串為空字串,則會返回原始字串。
- mb_strstr() 函式區分大小寫,如果要進行大小寫不敏感的搜尋,可以使用 mb_stripos() 函式。
熱門工具排行榜