PHP iconv_strlen() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iconv

Definition

In PHP, iconv_strlen() returns the number of characters in a string, using a character set you can choose.

Unlike strlen(), it counts a letter such as é as one character, not as two bytes.

Syntax

Call iconv_strlen() with the values you need. Start with this line.

PHP
$count = iconv_strlen($text, "UTF-8");
CallWhat it does
iconv_strlen("Héllo", "UTF-8")Returns 5.
iconv_strlen("caf\xE9", "ISO-8859-1")Returns 4.

If you leave out the encoding, the iconv internal encoding is used.

Library declarations
PHP
iconv_strlen(string $string, ?string $encoding = null): int|false

Parameters

ParameterDescription
stringRequired. The text to measure.
encodingOptional. The character set of the text. Default is the iconv internal encoding.

Return Value

Return ValueDescription
int|falseThe number of characters, or false if the text is not valid in that encoding.

PHP Examples

Example 1 — Count Characters

Compare iconv_strlen() with strlen().

PHP
<?php
$text = "Héllo";

echo strlen($text), PHP_EOL;
echo iconv_strlen($text, "UTF-8"), PHP_EOL;

How It Works

  1. Use a word with an accent. The letter é is 2 bytes.
  2. strlen() counts bytes. So it returns 6.
  3. iconv_strlen() counts characters. So it returns 5.
  4. Pass the encoding. It is safer than the default.

Example 2 — Catch an Invalid String

A byte that is not valid UTF-8 makes the function return false.

PHP
<?php
$bad = "caf\xE9";

var_dump(@iconv_strlen($bad, "UTF-8"));
var_dump(iconv_strlen($bad, "ISO-8859-1"));

How It Works

  1. Use a Latin-1 byte. \xE9 is not valid UTF-8 on its own.
  2. Measure it as UTF-8. The result is false.
  3. Measure it as ISO-8859-1. The result is 4.
  4. Pick the right encoding. It must match the text.

Example 3 — Check a Text Length

Use the character count to enforce a limit.

PHP
<?php
$name = "Zoë Müller";

if (iconv_strlen($name, "UTF-8") <= 10) {
    echo "Name fits", PHP_EOL;
}

How It Works

  1. Use a name with accents. It has 10 characters but 12 bytes.
  2. Count the characters. The limit is on characters, not bytes.
  3. Compare with 10. The count is 10, so the test passes.
  4. Use it for form checks. It is fair to users who type accents.

Frequently Asked Questions

iconv_strlen() returns the number of characters in a string, based on a character set.
iconv_strlen(string $string, ?string $encoding = null): int|false.
strlen() counts bytes. iconv_strlen() counts characters, so é counts as one.
It returns false when the string contains a byte that is not valid in the given encoding.
Both count characters. mb_strlen() is more common and does not depend on the system iconv library.

Next: iconv_strpos()

iconv_strpos() finds the character position of the first match in a string.

iconv_strpos() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful