Skip to content

Core\Str::length

Core\Str::length(string $s): uintCore\Str::length(
    string $s,
): uint

Counts the characters in a string, the way a person would count them.

An emoji, an accented letter or a flag each count as one character, even when the computer stores them as several bytes. Every Core\Str method counts the same way, so a position you get from one method is correct for every other one. The empty string has length 0.

Good to know: this is not the size of the string in memory. If you need bytes, for a file size or a network limit, use Core\Bytes::length. To check whether a string is empty, use Core\Str::isEmpty.

Parameters

  • $sstringThe string to measure.

Return value

Returns uint. The number of graphemes. The empty string gives 0.

Examples

Basics

<?nvs
echo Core\Str::length('hello'), "\n";
echo Core\Str::length(''), "\n";
Output
5
0

Graphemes not bytes

<?nvs
// `Core\Str::length` counts graphemes (what a person sees as one character).
// `héllo` has 5 graphemes, so the first line prints `5`. The family emoji is
// four emoji joined together. It is one grapheme, so the second line prints `1`.
echo Core\Str::length('héllo'), "\n";
echo Core\Str::length('👩‍👩‍👧‍👦'), "\n";
Output
5
1

Truncate a label

<?nvs
// Shortening a label to fit a column, without cutting a character in half.
// `length` counts characters the way a person sees them, so accented letters
// and emoji each count as one.
array<string> $labels = ['Order #1042', 'Café Größenwahn', 'Shipping — expedited'];
foreach ($labels as string $label) {
if (Core\Str::length($label) > 12) {
echo Core\Str::slice($label, 0, 11), "…\n";
} else {
echo $label, "\n";
}
}
Output
Order #1042
Café Größen…
Shipping — …

Validate a form field

<?nvs
// Checking a signup form the way a user experiences it: a display name of
// "Jo" is too short and a wall of emoji is too long, even though each emoji
// takes four bytes. Counting bytes would let one through and reject the other
// for the wrong reason.
class Signup
{
public static function check(string $name): string
{
var $n = Core\Str::length($name);
if ($n < 3) {
return 'too short (' . $n . ' characters, at least 3)';
}
if ($n > 16) {
return 'too long (' . $n . ' characters, at most 16)';
}
return 'ok';
}
}
array<string> $names = ['Jo', 'Ada Lovelace', '🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉'];
foreach ($names as string $name) {
echo Signup::check($name), "\n";
}
Output
too short (2 characters, at least 3)
ok
too long (17 characters, at most 16)

Pad a receipt column

<?nvs
// Lining up a receipt. A column's width is a count of characters, so the
// prices line up whether the item name is plain ASCII or not.
array<array<string>> $items = [
['Espresso', '2.40'],
['Café crème', '3.10'],
['Brötchen', '1.20'],
];
uint $width = 0;
foreach ($items as array<string> $item) {
var $n = Core\Str::length($item[0]);
if ($n > $width) {
$width = $n;
}
}
foreach ($items as array<string> $item) {
var $pad = $width - Core\Str::length($item[0]);
echo $item[0], Core\Str::repeat(' ', $pad), ' ', $item[1], "\n";
}
Output
Espresso 2.40
Café crème 3.10
Brötchen 1.20