webCOMAND

string_util::ellipsize()

Limit the length of a string to a certain maximum number of characters, adding an ellipsis or other string to represent the eliminated portion, which may be at the start, somewhere in the middle or end of the string.

Prototype

string ellipsize(string $str, integer $max_length, integer $position = 1, string $ellipsis = '\xe2\x80\xa6')

Parameters

  • str - Text string to operate on.
  • max_length - Maximum length of new string, not including the ellipsis.
  • position - Optional position within the string to replace characters exceeding the maximum length with the ellipsis, as a number in the range of 0 - 1.  0 indicates at the beginning of the string, 1 at the end, and a decimal like 0.25 or 0.5 indicates some amount from the start of the new string.  Default is 1, at the end of the string.
  • ellipsis - Optional text string that will replace the characters exceeding the maximum length.  The default is the UTF8 horizontal ellipsis character (…).

Return

The new string with characters exceeding the maximum length replaced by the ellipsis.

Example

// output: This is some lon…
$long = 'This is some long text to be ellipsized.';
$short = \io_comand_util\string_util::ellipsize($long, 16);
echo($short);