src/Drupal/Component/Utility/Unicode.php
public static function truncateBytes($string, $len) {
    if (strlen($string) <= $len) {
      return $string;
    }
    if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
      return substr($string, 0, $len);
    }
    // Scan backwards to beginning of the byte sequence.
    while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0);

    return substr($string, 0, $len);
  }
public static function truncateBytes($string, $len) {
    if (strlen($string) <= $len) {
      return $string;
    }
    if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
      return substr($string, 0, $len);
    }
    // Scan backwards to beginning of the byte sequence.
    while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0);

    return substr($string, 0, $len);
  }
public static function truncateBytes ( $ string , $ len ) { if ( strlen ( $ string ) <= $ len ) { return $ string ; } if ( ( ord ( $ string [ $ len ] ) < 0x80 ) || ( ord ( $ string [ $ len ] ) >= 0xC0 ) ) { return substr ( $ string , 0 , $ len ) ; } // Scan backwards to beginning of the byte sequence. while ( -- $ len >= 0 && ord ( $ string [ $ len ] ) >= 0x80 && ord ( $ string [ $ len ] ) < 0xC0 ) ; return substr ( $ string , 0 , $ len ) ; }
Truncates a UTF-8-encoded string safely to a number of bytes.

If the end position is in the middle of a UTF-8 sequence, it scans backwards
until the beginning of the byte sequence.

Use this function whenever you want to chop off a string at an unsure
location. On the other hand, if you're sure that you're splitting on a
character boundary (e.g. after using strpos() or similar), you can safely
use substr() instead.

@param string $string
The string to truncate.
@param int $len
An upper limit on the returned string length.

@return string
The truncated string.
Truncates a UTF - 8 - encoded string safely to a number of bytes .