mb_strcut
(PHP 4 >= 4.0.6, PHP 5)
mb_strcut — Get part of string
Description
$str
, int $start
[, int $length
= NULL
[, string $encoding
= mb_internal_encoding()
]] )mb_strcut() extracts a substring from a string similarly to mb_substr(), but operates on bytes instead of characters. If the cut position happens to be between two bytes of a multi-byte character, the cut is performed starting from the first byte of that character. This is also the difference to the substr() function, which would simply cut the string between the bytes and thus result in a malformed byte sequence.
Parameters
-
str
-
The string being cut.
-
start
-
Starting position in bytes.
-
length
-
Length in bytes. If omitted or NULL is passed, extract all bytes to the end of the string.
-
encoding
-
The
encoding
parameter is the character encoding. If it is omitted, the internal character encoding value will be used.
Return Values
mb_strcut() returns the portion of
str
specified by the
start
and
length
parameters.
See Also
- mb_substr() - Get part of string
- mb_internal_encoding() - Set/Get internal character encoding
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Поддержка языков и кодировок
- Многобайтные строки
- mb_check_encoding
- mb_convert_case
- mb_convert_encoding
- mb_convert_kana
- mb_convert_variables
- mb_decode_mimeheader
- mb_decode_numericentity
- mb_detect_encoding
- mb_detect_order
- mb_encode_mimeheader
- mb_encode_numericentity
- mb_encoding_aliases
- mb_ereg_match
- mb_ereg_replace_callback
- mb_ereg_replace
- mb_ereg_search_getpos
- mb_ereg_search_getregs
- mb_ereg_search_init
- mb_ereg_search_pos
- mb_ereg_search_regs
- mb_ereg_search_setpos
- mb_ereg_search
- mb_ereg
- mb_eregi_replace
- mb_eregi
- mb_get_info
- mb_http_input
- mb_http_output
- mb_internal_encoding
- mb_language
- mb_list_encodings
- mb_output_handler
- mb_parse_str
- mb_preferred_mime_name
- mb_regex_encoding
- mb_regex_set_options
- mb_send_mail
- mb_split
- mb_strcut
- mb_strimwidth
- mb_stripos
- mb_stristr
- mb_strlen
- mb_strpos
- mb_strrchr
- mb_strrichr
- mb_strripos
- mb_strrpos
- mb_strstr
- mb_strtolower
- mb_strtoupper
- mb_strwidth
- mb_substitute_character
- mb_substr_count
- mb_substr
Коментарии
diffrence between mb_substr and mb_substr
example:
mb_strcut('I_ROHA', 1, 2) returns 'I_'. Treated as byte stream.
mb_substr('I_ROHA', 1, 2) returns 'ROHA' Treated as character stream.
# 'I_' 'RO' 'HA' means multi-byte character
What the manual and the first commenter are trying to say is that mb_strcut uses byte offsets, as opposed to mb_substr which uses character offsets.
Both mb_strcut and mb_substr appear to treat negative and out-of-range offsets and lengths in the basically the same way as substr. An exception is that if start is too large, an empty string will be returned rather than FALSE. Testing indicates that mb_strcut first works out start and end byte offsets, then moves each offset left to the nearest character boundary.
Here is an example with UTF8 characters, to see how the start and length arguments are working:
$str_utf8 = utf8_encode("Déjà_vu");
$str_utf8_0 = mb_strcut($str_utf8, 0, 4, "UTF-8"); // Déj
$str_utf8_1 = mb_strcut($str_utf8, 1, 4, "UTF-8"); // éj
$str_utf8_2 = mb_strcut($str_utf8, 2, 4, "UTF-8"); // éj
$str_utf8_3 = mb_strcut($str_utf8, 3, 4, "UTF-8"); // jà_
$str_utf8_4 = mb_strcut($str_utf8, 4, 4, "UTF-8"); // à_v
The string includes two special charaters, "é" and "à" internally coded with two bytes.
Note that a multibyte character is removed rather than kept in half at the end of the output.
Note also that the result is the same for a cut 1,4 and a cut 2,4 with this string.
This was driving me crazy, because mb_strcut() kept returning an empty string. The $length parameter seems to have a max value of 2^32-1 (2147483647).
Works:
<?php
# output: Полуустав
echo mb_strcut('Полуустав', 0, pow(2,31)-1);
?>
Doesn't work:
<?php
# nothing is output
echo mb_strcut('Полуустав', 0, pow(2,31));
?>
My PHP_INT_MAX value is much larger than 2^32-1, so I'm not sure why larger values for $length don't work. :(
<?php
# output: 9223372036854775807
echo PHP_INT_MAX;
?>