Oihana PHP

truncate.php

Table of Contents

Functions

truncate()  : string
Truncates a string to a maximum number of grapheme clusters, appending an ellipsis.

Functions

truncate()

Truncates a string to a maximum number of grapheme clusters, appending an ellipsis.

truncate(string $source, int $length[, string $ellipsis = '…' ]) : string

The length is counted in grapheme clusters, so multibyte characters and combined emojis are never split. When the source is longer than $length, it is cut to the first $length graphemes and $ellipsis is appended (the ellipsis is not counted in $length). Shorter or equal strings are returned unchanged.

Parameters
$source : string

The input string.

$length : int

The maximum number of grapheme clusters to keep.

$ellipsis : string = '…'

The string appended when truncation occurs. Default .

Tags
example
use function oihana\core\strings\truncate;

echo truncate( 'The quick brown fox' , 9 ) ; // "The quick…"
echo truncate( 'Hello' , 10 )              ; // "Hello"
echo truncate( 'Café société' , 4 , '...' ); // "Café..."
author

Marc Alcaraz (ekameleon)

since
1.0.9
Return values
string

The truncated string, or the original when no truncation is needed.

On this page

Search results