函式名:mb_stristr()
適用版本:PHP 4.0.6及以上版本
用法:mb_stristr()函式用於在一個字串中查詢指定的子字串,並返回從指定子字串到字串結尾的部分。該函式是對stristr()函式的多位元組字元安全版本。
語法:mb_stristr(string $haystack, string $needle, bool $before_needle = false, string $encoding = mb_internal_encoding()): string|false
引數:
- $haystack:要搜尋的字串。
- $needle:要搜尋的子字串。
- $before_needle(可選):如果設定為true,則返回指定子字串之前的部分;如果設定為false(預設),則返回指定子字串之後的部分。
- $encoding(可選):指定字串的字元編碼。如果未提供此引數,則預設使用mb_internal_encoding()函式返回的內部編碼。
返回值:
- 如果找到了指定子字串,則返回從該子字串到字串結尾的部分。
- 如果未找到指定子字串,則返回false。
示例:
$str = "Hello, World!";
$substring = "WORLD";
$result = mb_stristr($str, $substring);
echo $result; // 輸出:World!
$result = mb_stristr($str, $substring, true);
echo $result; // 輸出:Hello,
$result = mb_stristr($str, "abc");
var_dump($result); // 輸出:bool(false)
在上面的示例中,我們使用mb_stristr()函式搜尋字串$str中的子字串$substring。第一個示例中,函式返回從子字串World到字串結尾的部分,即World!。第二個示例中,我們將第三個引數設定為true,函式返回子字串World之前的部分,即Hello, 。最後一個示例中,我們搜尋了一個不存在的子字串abc,函式返回false。
熱門工具排行榜