Home >Backend Development >PHP Tutorial >How to Safely Truncate UTF-8 Strings in PHP While Preserving Word Boundaries?
Truncating Strings with UTF-8 Characters
Problem:
Truncating multibyte strings to a specified character limit while preserving word boundaries can be a challenge in PHP. This issue involves achieving this functionality with a custom method named truncate() that should behave consistently with multibyte characters.
Steps to Resolve:
Solution Using mb_strimwidth():
PHP provides the mb_strimwidth() function, which can handle multibyte string truncation. This function does not, however, obey word boundaries. The following code snippet demonstrates its usage:
public function truncate($string, $chars = 50, $terminator = ' …') { $maxChars = $chars - strlen($terminator); if (mb_strlen($string) <= $maxChars) { return $string; } $lastWhitespace = mb_strrpos(mb_substr($string, 0, $maxChars), ' '); if ($lastWhitespace !== false) { return mb_substr($string, 0, $lastWhitespace) . $terminator; } else { return mb_substr($string, 0, $maxChars) . $terminator; } }
The above is the detailed content of How to Safely Truncate UTF-8 Strings in PHP While Preserving Word Boundaries?. For more information, please follow other related articles on the PHP Chinese website!