PHP – 如何使用 mb_substr() 獲取字串的選定部分?
在 PHP 中,**mb_substr()** 用於返回給定字串的選定部分。多位元組安全的 **substr()** 基於字元數量工作。它從字串的開頭計算位置。它將返回第一個字元位置的 0 和第二個字元位置的 1,依此類推。
語法
string mb_substr(str $string, int $start, int $length, str $encoding)
引數
此 PHP 函式接受四個引數:**$string**、**$start**、**$length** 和 **$encoding**。
**$string−** 此引數用於從給定字串中提取子字串。
$string = mb_substr("Welcome to the online tutorials!", 5, 10, "UTF-8");
**$start−** 如果 start 為非負數,則此引數從開頭返回第一個字元的 0。例如,如果給定字串為 **“abcefg”**,則第一個位置的字元為 0,即 **“a”**,依此類推。如果 start 字串為負數,則它將返回字串末尾的字元。
**$length−** length 引數是從字串中使用的最大字元數。
// Length is used from character (5 to 10) (5, 10, "UTF-8");
**$encoding− **它用於字元編碼。如果省略或為 null,則將使用內部字元編碼值。
返回值
多位元組子字串函式將使用 **start** 和 **length** 引數從給定字串中返回選定的部分。
示例
<?php // the mb_substr function will return // the selected part of string $string = mb_substr("Welcome to the online tutorials!", 5, 10, "UTF-8"); // Convert selected string in upper case $string = mb_convert_case($string, MB_CASE_UPPER, "UTF-8"); // Output will be me to the echo "$string"; ?>
輸出
ME TO THE
廣告