Logo Luan Morina
Truncate a PHP string to a specified width

To truncate a PHP string to a specified width, you can use the mb_strimwidth() function. Here's an example:

PHP

<?php
$demo_string = "<p>A cruise ship passes by a remote island, and all the passengers see a bearded man running around and waving his arms wildly. “Captain,” one passenger asks, “who is that man over there?” “I have no idea,” the captain says, “but he goes nuts every year when we pass him.”<p>";
echo mb_strimwidth($demo_string, 0, 210, '...');
?>

Demo

A cruise ship passes by a remote island, and all the passengers see a bearded man running around and waving his arms wildly. “Captain,” one passenger asks, “who is that man over there?” “I have no idea,” ...

   Truncate a string to a specified width