CKTIS / PHP
mb_strlen()
Use mb_strlen to count characters in the specified encoding rather than raw bytes; null selects the internal encoding.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
mb_strlen(string $string, ?string $encoding = null): intUse mb_strlen to count characters in the specified encoding rather than raw bytes; null selects the internal encoding. In UTF-8 it counts code points, so a combining mark counts separately from its base even when they form one visible letter. The empty string counts zero. An unknown encoding name raises ValueError in PHP 8.4.
Signature
mb_strlen(string $string, ?string $encoding = null): int
Example output
[2,3,1,0]
ValueError
Examples
<?php
$s = "e\u{0301}";
echo json_encode([mb_strlen($s, "UTF-8"), strlen($s), mb_strlen("é", "UTF-8"), mb_strlen("", "UTF-8")]), PHP_EOL;
try { mb_strlen("x", "not_an_encoding"); } catch (ValueError $e) { echo get_class($e); }
Result
[2,3,1,0] ValueError