Correct

if (mb_strlen($name, 'UTF-8') < 3) is sufficient enough

make sure header is correct

HTTP-header (Content-Type: text/html; charset=UTF-8)

you can also check alternative for some reason

strlen(utf8_decode($string))

UTF-8 to Code Point Array Converter in PHP

Answer from Pramendra Gupta on Stack Overflow
🌐
PHP
php.net › manual › en › function.mb-strlen.php
PHP: mb_strlen - Manual
If you need length of string in ... '8bit'); ?>. It's the fastest way (still a way slower than strlen, though) to determine byte length of string....
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-mb_strlen-function
PHP mb_strlen() Function - GeeksforGeeks
April 13, 2023 - The mb_strlen() is an inbuilt PHP function that returns the string length in an integer.
🌐
Code.mu
code.mu › en › php › manual › string › mb_strlen
The mb_strlen Function - Getting String Length in PHP
The mb_strlen function returns the number of characters in a string, working correctly with multibyte encodings (for example, UTF-8). Unlike strlen, it correctly counts characters that occupy multiple bytes. It takes the string as the first parameter and the encoding as the second (optional) one.
🌐
Illinois State Museum
museum.state.il.us › ismdepts › library › linuxguides › php › function.mb-strlen.html
mb_strlen
(PHP 4 CVS only)mb_strlen -- Get string length · string mb_strlen (string str [, string encoding]) mb_strlen() returns number of characters in string str having character encoding encoding. A multi-byte character is counted as 1.
🌐
Quora
quora.com › What-is-difference-between-mb_strlen-and-strlen-in-PHP
What is difference between mb_strlen() and strlen() in PHP? - Quora
Answer (1 of 10): Ascii characters are 1 byte in length. [code ]strlen[/code] function assumes that every character is 1 byte in length, which works fine for English language. But when you start dealing with non English languages, characters can take more than 1 byte to represent. The number of ...
🌐
OnlinePHP
onlinephp.io › mb-strlen
mb_strlen - Online Tool
PHP Functions · Multibyte String · mb_strlen · Execute mb_strlen with this online tool mb_strlen() - Get string length · Mb Strlen Online Tool · Manual · Code Examples · Mb Strlen Online Tool · Manual · Code Examples · mb_check_encoding · mb_chr · mb_convert_case ·
🌐
Php-safari
php-safari.com › function › mb_strlen
PHP function - mb_strlen | PHP Safari
mb_strlen($runTime); $width = min(terminal()->width(), 150); $dots = max($width - $descriptionWidth - $runTimeWidth - 10, 0);
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › php-get-the-string-length-using-mb-strlen
PHP – Get the string length using mb_strlen()
August 23, 2021 - In PHP, multibyte string length (mb_strlen) function is used to get the total string length of a specified string. This function is supported in PHP 4.6.0 or higher versions. Syntax
🌐
OnlinePHP
onlinephp.io › mb-strlen › manual
mb_strlen - OnlinePHP.io Example
Execute mb_strlen Online. Info and examples on mb_strlen PHP Function from Multibyte String - Human Language and Character Encoding Support
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php strlen
PHP strlen(): Get the Length of a String in Bytes Examples
April 7, 2025 - Use the PHP strlen() function to get the number of bytes of a string. Use the PHP mb_strlen() function to get the number of characters in a string with a specific encoding.
🌐
Reintech
reintech.io › term › using-mb-strlen-for-multibyte-strings-php
Using the mb_strlen() Function for Multibyte Strings in PHP | Reintech media
This function measures the length of a string with multibyte characters. Unlike strlen(), it accounts for character encodings like UTF-8 where characters can be represented by multiple bytes.
🌐
Abanoub Hanna
abanoubhanna.com › posts › mb_strlen-or-strlen-php
Should I use `mb_strlen($text, 'utf-8')` or `strlen($text)` in PHP ? | Abanoub Hanna
April 15, 2024 - 🔗 Accuracy for Multibyte Characters: mb_strlen is designed for handling multibyte character encodings like UTF-8. It considers characters that can be composed of multiple bytes, giving you the correct character count. strlen assumes single-byte characters and might undercount the length ...
🌐
Cakephp
api.cakephp.org › 2.10 › function-mb_strlen.html
Function mb_strlen | CakePHP 2.10
Package: Cake Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) License: MIT License Located at Cake/bootstrap.php · mb_strlen( string $string , string $encoding = null )
🌐
Nusphere
nusphere.com › kb › phpmanual › function.mb-strlen.htm
PHP Manual: mb_strlen
PhpED - PHP IDE integrated development environment for developing web sites using PHP, HTML, Perl, JScript and CSS that combines a comfortable editor, debugger, profiler with the MySQl, PostrgeSQL database support based on easy wizards and tutorials.Easy to use for debugging PHP scripts, publishing ...
Top answer
1 of 3
22

To make it short: you do not really know about the encoding (character set) used on the variables that are passed to your PHP script via GET or POST (especially GET is a problem here). By convention browsers POST forms to the server-side resource specified in the action-attribute using the page encoding which can be specified via an http-equiv-meta-tag (charset-meta-tag in HTML5) or via an HTTP header. Alternatively some browsers also respect the accept-charset-attribute on the form when chosing the correct encoding.

The encoding of GET parameters and the URL itself depends on the browser stettings and can therefore be controlled by the user. You should not rely on a specific encoding.

Generally you'll circumnavigate most encoding-related problems by consistently using UTF-8 for everything and by specifying the correct encoding in the HTTP-header (Content-Type: text/html; charset=UTF-8) - this will yield the correct encoding (UTF-8) in all the variables that are passed into your string (we're not talking about rouge scripts that deliberately try to mess with the encoding to allow for some attack vectors into your script). You also should not rely on non-ascii-characters in your GET parameters or in the URL (that's also a reason why SEO-friendly links remove those characters or substitute them).

If you made sure that UTF-8 is the only allowed character-set you can use mb_strlen($string, 'UTF-8') to check the length of a variable for example.

EDIT: (added some links)

Some things for you to read:

  • The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
  • Handling UTF-8 with PHP
2 of 3
5

use mb_internal_encoding to know which encoding is currently set. If you application use a log of different encoding you have better to use mb_strlen.

Cheers