Current Location: Home> Function Categories> mb_strimwidth

mb_strimwidth

Get string truncated by specified width
Name:mb_strimwidth
Category:Multi-byte string
Programming Language:php
One-line Description:Truncate the string according to the specified width and add an optional modifier at the end

Function name: mb_strimwidth()

Applicable version: PHP 4 >= 4.0.6, PHP 5, PHP 7

Usage: mb_strimwidth(string $str, int $start, int $width, string $trimmarker = "", string $encoding = null): string

Description: The mb_strimwidth() function truncates the string based on the specified width and adds an optional modifier at the end. This function is multibyte-safe and can correctly handle strings containing multibyte characters.

parameter:

  • $str: The string to truncate.
  • $start: The starting position of truncation, which can be a negative number.
  • $width: The truncated string length.
  • $trimmarker (optional): Added to the end of the truncated string, defaulting to an empty string.
  • $encoding (optional): Specify character encoding, default to internal character encoding.

Return value: Returns the truncated string.

Example:

 $text = "这是一个示例文本,用于演示mb_strimwidth()函数的用法"; $trimmedText = mb_strimwidth($text, 0, 20, "..."); echo $trimmedText; // 输出:这是一个示例文本...

In the example above, we use the mb_strimwidth() function to truncate the text into a string of length 20, and if truncated, the ellipsis "..." is added at the end.

Similar Functions