mb_strcut

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

mb_strcutObtém parte da string

Descrição

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

mb_strcut() extrai uma substring de uma string de forma semelhante a mb_substr(), mas opera em bytes em vez de caracteres. Se a posição de corte estiver entre dois bytes de um caractere multibyte, o corte é realizado a partir do primeiro byte desse caractere. Essa também é a diferença para a função substr(), que simplesmente cortaria a string entre os bytes e resultaria em uma sequência de bytes malformada.

Parâmetros

string

A string a ser cortada.

start

Se start for não negativo, a string retornada começará na posição de byte start em string, contando a partir do zero. Por exemplo, na string 'abcdef', o byte na posição 0 é 'a', o byte na posição 2 é 'c', e assim por diante.

Se start for negativo, a string retornada começará no byte start contando a partir do final de string. No entanto, se o valor absoluto de um start negativo for maior que o comprimento da string, a parte retornada começará do início de string.

length

Comprimento em bytes. Se omitido ou NULL for passado, extrai todos os bytes até o final da string.

Se length for negativo, a string retornada terminará no byte de número length contando a partir do final de string. No entanto, se o valor absoluto de um length negativo for maior que o número de caracteres após a posição de start, uma string vazia será retornada.

encoding

O parâmetro encoding é a codificação de caracteres. Se for omitido ou null, o valor da codificação de caracteres interna será usado.

Valor Retornado

mb_strcut() retorna a parte de string especificada pelos parâmetros start e length.

Registro de Alterações

Versão Descrição
8.0.0 O parâmetro encoding agora pode ser nulo.

Veja Também

add a note add a note

User Contributed Notes 4 notes

up
6
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
2
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
2
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
-20
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