18

I have some strings in my PHP code that need to be truncated if they are too long.

For example if a text is something like this:

Hi, I would like to tell you how wonderful this is.

It would replace it with this:

Hi, I would like to ...

For that I've done a simple substr. The problem is that in UTF8 some characters are actually two characters long. And I've had some problems with a character being cut in the middle: For example, when I try to insert the modified string in the database, it crashes.

Here is my current function:

static function short($string, $max = 255){
   if(strlen($string) >= $max){
       $string = substr($string, 0, $max - 5).'...';
   } return $string;
}

Would someone know a way to make this function work even for UTF8 characters?

Dennis
  • 14,264
  • 2
  • 48
  • 57
Andrei
  • 1,183
  • 2
  • 19
  • 40
  • have a look at `https://api.drupal.org/api/drupal/includes!unicode.inc/function/truncate_utf8/7` – Mohammad Ali Akbari Jun 08 '13 at 08:36
  • possible duplicate of [UTF-8 compatible truncate function](http://stackoverflow.com/questions/6288875/utf-8-compatible-truncate-function) – user Mar 16 '14 at 23:40
  • 1
    You might find [`s($str)->truncate($length)`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L233) and [`s($str)->truncateSafely($length)`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L246) helpful, as found in [this standalone library](https://github.com/delight-im/PHP-Str). Both versions are Unicode-safe. The latter does not break words. – caw Jul 27 '16 at 02:58

2 Answers2

25

Everything you need is mb_strimwidth() : http://php.net/manual/en/function.mb-strimwidth.php

Example:

mb_strimwidth('Hi, I would like to tell you how wonderful this is.',0,15,'...','utf-8');
Timur
  • 6,668
  • 1
  • 28
  • 37
  • 1
    @trejder No, it isn't. *The initial version of this article was claiming that mb_strimwidth() is deprecated in PHP 7.2 and that this deprecation was a source of described problem. It turned out that mb_strimwidth() is (of course!) not deprecated in PHP 7.2.* – Álvaro González Jul 27 '18 at 07:57
  • @ÁlvaroGonzález Correct! I'm removing my comment. Thanks! – trejder Jul 30 '18 at 11:47
8

try with mb_substr() :

static function short($string, $max = 255){
   if(mb_strlen($string, 'utf-8') >= $max){
       $string = mb_substr($string, 0, $max - 5, 'utf-8').'...';
   } return $string;
}
Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72