Skip to content
PHP Logo

What are some best practices for efficiently trimming text in PHP without affecting readability?

When trimming text in PHP, it's important to maintain readability while efficiently removing excess characters. One way to achieve this is by using the `mb_strimwidth()` function, which trims a string to a specified width while preserving multibyte characters. This ensures that the trimmed text remains visually intact and readable.

php
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
$trimmed_text = mb_strimwidth($text, 0, 20, '...');

echo $trimmed_text; // Output: Lorem ipsum dolor...