PHP – mb_strcut() function


The mb_strcut() function in PHP is used to get a part of a specified string. It extracts the substring from a given string. It operates on bytes instead of characters. If the cut position happens to be between two bytes of multi-byte characters, then the cut is achieved starting from the first byte of those characters.

Syntax

string mb_strcut(
   $str_string,
   $int_start,
   $int_length=null,
   $str_encoding=null
);

For example:

mb_strcut(
   string="Onlinetutorial",
   int= 6,
   length=5,
   encoding= "UTF-8"
);

Parameters

mb_strcut() accepts the following four parameters −

  • str_string − The string parameter being cut.

  • int_start − If the start parameter is non-negative, then the returned string will start from the starting byte position in a given string, counting from zero. For example, in a string "onlinetutorial", the byte at position '0' is 'o', and the byte at position '1' is 'n' and so on. If the start is negative, then the given string will start counting back from the end of the string.

  • If the magnitude of the negative start is greater than the string length, then the returned portion will start extracting from the beginning of the string.

  • int_length − It returns the length in bytes. If the length is omitted, then NULL is passed, it extracts all the bytes up to the end of the string. If the given length is negative, then the returned string will end at the length byte containing back from the end of the string.

  • encoding − It is the character encoding parameter. If it is omitted or NULL, then the internal encoding value is used.

Return Values

mb_strcut() returns the specified portion of the string by the start and length parameters.

Example

<?php
   //UTF-8 encoding
   //String online tutorial will cut the string from 6
   // to till 5 characters
   $string = mb_strcut("Onlinetutorial", 6, 5, "UTF-8");
   $str = mb_strtoupper($string);

   //output "TUTOR"
   print_r($str);
?>

Output

TUTOR

Updated on: 12-Oct-2021

374 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements