PHP 8.3.4 Released!

mb_strcut

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

mb_strcutCoupe une partie de chaîne

Description

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

mb_strcut() extrait une sous-chaîne depuis une chaîne, d'une façon similaire à la fonction mb_substr(), mais opère sur les octets au lieu des caractères. Si le découpage intervient entre 2 octets d'un caractère multioctets, le découpage sera effectué au début du premier octet de ce caractère. C'est également la différence avec la fonction substr() qui coupera la chaîne au milieu des octets, résultant ainsi en une séquence d'octets mal-formée.

Liste de paramètres

string

La chaîne à couper.

start

Si start est positif, la chaîne retournée commencera à l'octet numéro start, dans la chaîne string. Le premier caractère est numéroté zéro. En effet, dans la chaîne 'abcdef', l'octet à la position 0 est 'a', l'octet à la position 2 est 'c', et ainsi de suite.

Si start est négatif, la chaîne retournée commencera à l'octet numéro start à compter de la fin de la chaîne string. Cependant, si le nombre négatif passé en paramètre start est supérieur à la longueur de la chaîne, la portion retournée commencera depuis le début de la chaîne string.

length

Longueur en octets. Si ce paramètre est omis, ou vaut NULL, tous les octets jusqu'à la fin de la chaîne seront extraits.

Si length est négatif, la chaîne retournée se finira à la position length en comptant en arrière depuis la fin de la chaîne string. Cependant, si le nombre négatif passé au paramètre length est plus grand que le nombre de caractères après la position start, une chaîne vide sera retourné.

encoding

Le paramètre encoding est l'encodage des caractères. S'il est omis ou null, l'encodage de caractères interne sera utilisé.

Valeurs de retour

mb_strcut() retourne la portion de la chaîne string qui commence au caractère start et a la longueur de length caractères.

Historique

Version Description
8.0.0 encoding est désormais nullable.

Voir aussi

add a note

User Contributed Notes 5 notes

up
1
David Juhasz
2 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
3
olivthill at gmail dot com
6 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
19 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
-1
oyag02 at yahoo dot co dot jp
20 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
up
-29
php_engineer_bk at yahoo dot com
13 years ago
function cut_sense($matne_harf, $l_harf ,$return=1 ) {
if ( strlen($matne_harf) > $l_harf){
$end='...';
}else{
$end='';
}
if ( function_exists('mb_strcut') ){
$matne_harf = mb_strcut ( $matne_harf, 0 , $l_harf , "UTF-8" );
}else{
$matne_harf =substr($matne_harf, 0, $l_harf);
}
$text=''.$matne_harf.''.$end.'';
if ( $return == 1){
return $text;
}else{
print $text;
}
}

Iranian php programmer (farhad zand +989383015266)
To Top