Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

mb_strimwidth

Truncates a string to a given width and appends a marker.

Syntax

mb_strimwidth(string $string, int $start, int $width, string $trim_marker = ""): string

Parameters

ParameterTypeDescription
$stringstringThe source string.
$startintThe position to start at.
$widthintThe maximum width of the result including the marker.
$trim_markerstringOptional. What to append to a truncated string.

Return value

stringThe truncated string.

Examples

A post teaser with an ellipsis:

<?php
 
echo mb_strimwidth('A very long title of an article', 0, 15, '...');

Output:

A very long ...

A short string is left alone:

<?php
 
echo mb_strimwidth('Short', 0, 15, '...');

Output:

Short

Notes

The marker counts towards the width. A string that already fits is returned without the marker.

See also

  • mb_substr — Returns part of a string.
  • mb_strlen — Returns the number of characters in a string.

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.