(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)
mb_strcut — Corta una parte de string
$string,$start,$length = null ,$encoding = null 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.
stringEl 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.
lengthnull ,
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.
encodingencoding
es la codificación de caracteres. Si se omite o es null , se utilizará el valor
de la codificación de caracteres interna.
mb_strcut() devuelve la porción del string
string que comienza en el carácter
start y tiene la longitud de
length caracteres.
| Versión | Descripción |
|---|---|
| 8.4.0 |
El comportamiento ahora es más consistente con las cadenas UTF-8 y UTF-16 inválidas.
|
| 8.0.0 |
encoding ahora acepta null .
|
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.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.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;
?>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