Page MenuHomePhorge
Diviner libphutil Tech Docs PhutilUTF8StringTruncator

final class PhutilUTF8StringTruncator
libphutil Technical Documentation (Core Utilities)

Truncate a UTF-8 string to a some maximum number of bytes, codepoints, or glyphs.

This class takes some UTF-8 string as input, and emits a valid UTF-8 string which is no longer than given byte, codepoint, or glyph limits.

$short = id(new PhutilUTF8StringTruncator())
  ->setMaximumGlyphs(80)
  ->truncateString($long);

Byte limits restrict the number of bytes the result may contain. They are appropriate when you care about how much storage a string requires.

Codepoint limits restrict the number of codepoints the result may contain. Since codepoints may have up to 4 bytes, the resulting strings may require have more than this many bytes. This kind of limit is appropriate when you are using UTF-8 storage.

Glyph limits restrict the display size of the string. Because a single glyph may have an arbitrary number of combining characters, this does not impose a storage size limit on the string: a string with only one glyph may require an arbitrarily large number of bytes.

You can set more than one limit; the smallest limit will be used.

NOTE: This function makes a best effort to apply some reasonable rules but will not work well for the full range of unicode languages.

Methods

public function __get($name)
Inherited

This method is not documented.
Parameters
$name
Return
wild

public function __set($name, $value)
Inherited

This method is not documented.
Parameters
$name
$value
Return
wild

public function current()
Inherited

This method is not documented.
Return
wild

public function key()
Inherited

This method is not documented.
Return
wild

public function next()
Inherited

This method is not documented.
Return
wild

public function rewind()
Inherited

This method is not documented.
Return
wild

public function valid()
Inherited

This method is not documented.
Return
wild

private function throwOnAttemptedIteration()
Inherited

This method is not documented.
Return
wild

public function getPhobjectClassConstant($key, $byte_limit)
Inherited

Phobject

Read the value of a class constant.

This is the same as just typing self::CONSTANTNAME, but throws a more useful message if the constant is not defined and allows the constant to be limited to a maximum length.

Parameters
string$keyName of the constant.
int|null$byte_limitMaximum number of bytes permitted in the value.
Return
stringValue of the constant.

public function setMaximumBytes($maximum_bytes)

This method is not documented.
Parameters
$maximum_bytes
Return
wild

public function setMaximumCodepoints($maximum_codepoints)

This method is not documented.
Parameters
$maximum_codepoints
Return
wild

public function setMaximumGlyphs($maximum_glyphs)

This method is not documented.
Parameters
$maximum_glyphs
Return
wild

private function didUpdateMaxima()

This method is not documented.
Return
wild

public function setTerminator($terminator)

This method is not documented.
Parameters
$terminator
Return
wild

public function truncateString($string)

This method is not documented.
Parameters
$string
Return
wild