function Unicode::truncateBytes
Same name in other branches
- 9 core/lib/Drupal/Component/Utility/Unicode.php \Drupal\Component\Utility\Unicode::truncateBytes()
- 8.9.x core/lib/Drupal/Component/Utility/Unicode.php \Drupal\Component\Utility\Unicode::truncateBytes()
- 11.x core/lib/Drupal/Component/Utility/Unicode.php \Drupal\Component\Utility\Unicode::truncateBytes()
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.
Parameters
string $string: The string to truncate.
int $len: An upper limit on the returned string length.
Return value
string The truncated string.
2 calls to Unicode::truncateBytes()
- ExceptionHandler::handleExecutionException in core/
modules/ mysql/ src/ Driver/ Database/ mysql/ ExceptionHandler.php - UnicodeTest::testTruncateBytes in core/
tests/ Drupal/ Tests/ Component/ Utility/ UnicodeTest.php - Tests multibyte truncate bytes.
File
-
core/
lib/ Drupal/ Component/ Utility/ Unicode.php, line 209
Class
- Unicode
- Provides Unicode-related conversions and operations.
Namespace
Drupal\Component\UtilityCode
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.
// @todo Make the code more readable in https://www.drupal.org/node/2911497.
while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xc0) {
}
return substr($string, 0, $len);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.