We limited task titles to 120. Short English titles looked fine, but titles with non-ASCII letters can hit the limit earlier than expected. Is the title really too long, or are we counting something else?
Start with a tiny Turkish word: “Çay.” Create experiment.php in a new folder. This lesson needs the mbstring extension. Run php -m in the terminal and check for mbstring before saving the example.
Three letters, but the first result is four
<?php
$title = 'Çay';
echo strlen($title), PHP_EOL;
echo mb_strlen($title, 'UTF-8'), PHP_EOL;
Run php experiment.php: it prints 4, then 3. In this UTF-8 string, Ç occupies two bytes while a and y occupy one each. strlen() counts bytes. mb_strlen($title, 'UTF-8') counts characters in the specified encoding, giving 3.
strlen is not calculating incorrectly; it measures something different. A byte count may be exactly what you need for a payload length. But if the interface promises “at most 120 characters,” the validation should reflect that decision.
If mb_strlen is missing, fix the environment first
If PHP reports Call to undefined function mb_strlen(), enable the extension rather than changing the experiment back to strlen. On Windows, use php --ini to locate the loaded php.ini. In your local installation, uncomment extension=mbstring if needed, and ensure extension_dir points to the PHP ext directory. Run php -m again. If no configuration file is loaded, first create a php.ini appropriate for your PHP distribution.
On Ubuntu/Debian installations using distribution PHP packages, install the matching php-mbstring package. With multiple PHP versions, match the extension to the interpreter used by the command. Installing a package for one interpreter does not enable it in every other interpreter. This terminal experiment requires mbstring in the CLI module list; a web PHP installation has its own configuration to check.
Expose the meaning of the 120 limit
<?php
$title = str_repeat('Ç', 70);
var_dump(strlen($title));
var_dump(mb_strlen($title, 'UTF-8'));
var_dump(strlen($title) <= 120);
var_dump(mb_strlen($title, 'UTF-8') <= 120);
str_repeat('Ç', 70) creates 70 copies of the character, saving us from typing a long title. The results are int(140), int(70), false, and true. The title exceeds the byte limit but fits the character limit.
The original task-list validator used strlen($title) <= 120, which imposed a byte limit. To adopt a character-based rule, use mb_strlen($title, 'UTF-8'). That changes the rule intentionally. Keeping the number 120 does not make the two checks equivalent.
Put the new rule into a small validator
<?php
function validateTitle(mixed $value): ?string
{
if (!is_string($value)) {
return null;
}
$title = trim($value);
if ($title === '' || mb_strlen($title, 'UTF-8') > 120) {
return null;
}
return $title;
}
var_dump(validateTitle(' '));
var_dump(validateTitle(['Task']));
var_dump(validateTitle(str_repeat('Ç', 120)) !== null);
var_dump(validateTitle(str_repeat('Ç', 121)) === null);
Replace the entire experiment file with this code and run it. The first two results are NULL: whitespace-only input and an array are rejected. The next two are bool(true): 120 copies of Ç are accepted and 121 are rejected. !== null checks that the first long input returned a title; === null checks that the second was rejected.
This is a separate experimental validateTitle(). To adopt the rule in the task-list project, replace strlen($title) with mb_strlen($title, 'UTF-8') in the existing function in app/functions.php. Do not add a second function definition. Keep the demonstration's var_dump calls in the experiment file.
One visible mark is not always one code point
<?php
$single = "\u{00E9}";
$combined = "e\u{0301}";
echo $single, ' / ', $combined, PHP_EOL;
echo mb_strlen($single, 'UTF-8'), PHP_EOL;
echo mb_strlen($combined, 'UTF-8'), PHP_EOL;
The two versions look similar: é / é. The first uses a single Unicode code point; the second uses an e followed by a combining accent. Their mb_strlen results are therefore 1 and 2.
So do not describe mb_strlen as always counting exactly the symbols a person sees on screen. Joined emoji and combining sequences introduce grapheme clusters, a separate question. Our chosen rule here is length as measured by mb_strlen for UTF-8 input. Finish by checking your own non-ASCII titles at the 120/121 boundary and confirm which rule your application enforces.
Where this appeared in the task list: PHP Lesson 03 — Read and Validate a Task Form
Official documentation: strlen · mb_strlen · mbstring installation · str_repeat