PHP 8.5.0 Alpha 1 available for testing

mb_strcut

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

mb_strcutCorta una parte de string

Descripción

mb_strcut(
    string $string,
    int $start,
    ?int $length = null,
    ?string $encoding = null
): string

mb_strcut() extrae un substring desde un string, de manera similar a la función mb_substr(), pero opera sobre los bytes en lugar de los caracteres. Si el corte ocurre entre 2 bytes de un carácter multibyte, el corte se realizará al inicio del primer byte de ese carácter. Esta es también la diferencia con la función substr() que cortará el string en medio de los bytes, resultando en una secuencia de bytes mal formada.

Parámetros

string

El string a cortar.

start

Si start es positivo, el string devuelto comenzará en el byte número start, en el string string. El primer carácter está numerado cero. En efecto, en el string 'abcdef', el byte en la posición 0 es 'a', el byte en la posición 2 es 'c', y así sucesivamente.

Si start es negativo, el string devuelto comenzará en el byte número start contando desde el final del string string. Sin embargo, si el número negativo pasado como argumento start es mayor que la longitud del string, la porción devuelta comenzará desde el inicio del string string.

length

Longitud en bytes. Si este argumento es omitido, o vale NULL, todos los bytes hasta el final del string serán extraídos.

Si length es negativo, el string devuelto terminará en la posición length contando desde el final del string string. Sin embargo, si el número negativo pasado al argumento length es mayor que el número de caracteres después de la posición start, un string vacío será devuelto.

encoding

El parámetro encoding es la codificación de caracteres. Si es omitido o null, será usado el valor de la codificación de caracteres interna.

Valores devueltos

mb_strcut() devuelve la porción del string string que comienza en el carácter start y tiene la longitud de length caracteres.

Historial de cambios

Versión Descripción
8.0.0 encoding ahora es nullable.

Ver también

add a note

User Contributed Notes 4 notes

up
4
olivthill at gmail dot com
7 years ago
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.
up
4
t dot starling at physics dot unimelb dot edu dot au
20 years ago
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.
up
0
David Juhasz
3 years ago
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;
?>
up
-2
oyag02 at yahoo dot co dot jp
21 years ago
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
To Top