PHP mb_strimwidth() Function

The mb_strimwidth() function is an inbuilt function in PHP that returns the truncated string for the specified width.

Syntax:

string mb_strimwidth($string, $start, $width, $trim_marker, $encoding )

Parameters: This function accepts five parameters that are described below:

  • $string: This parameter specifies the string that will be trimmed.
  • $start: This is the starting point where you start the trim. The beginning of the string (index starts from 0). If the start initializes negative, it will start from the ending of the string.
  • $width: This parameter specifies the desired width of the trimmed string. In case, if the with is negative then the count will be from the end of the string.
  • $trim_maker: This parameter specifies a string is added to the end of the trimmed string.
  • $encoding: The character encoding is to be used. By default, it will use UTF-8. The encoding parameter denotes the character encoding. If omitted or null, then the internal character encoding value will be utilized.

Return Values: This function returns the truncated string.

Example 1: The following program demonstrates the mb_strimwidth() function.

PHP




<?php
  
$str = "This is a very long string that needs to be trimmed";
$trimmed_str = mb_strimwidth($str, 0, 20, "...");
echo $trimmed_str;
  
?>


Output:

This is a very lo...  

Example 2: The following program demonstrates the mb_strimwidth() function.

PHP




<?php
  
$str = "Beginner for Beginner Matter";
$trimmed_str = mb_strimwidth($str, -5,5 , "Gee");
echo $trimmed_str;
  
?>


Output:

atter 

Reference: https://www.php.net/manual/en/function.mb-strimwidth.php