PHP iconv_substr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iconv

Definition

In PHP, iconv_substr() returns a part of a string, counted in characters, using a character set you can choose.

Unlike substr(), it never cuts an accented letter in half.

Syntax

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

PHP
$part = iconv_substr($text, 0, 5, "UTF-8");
CallWhat it does
iconv_substr("Héllo Wörld", 0, 5, "UTF-8")Returns "Héllo".
iconv_substr("Héllo Wörld", -5, null, "UTF-8")Returns "Wörld".

A negative offset counts from the end of the string. The first character is at position 0.

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

Parameters

ParameterDescription
stringRequired. The source text.
offsetRequired. The start position. A negative value counts from the end.
lengthOptional. The number of characters to return. Default is to the end.
encodingOptional. The character set. Default is the iconv internal encoding.

Return Value

Return ValueDescription
string|falseThe part of the string, or false on failure.

PHP Examples

Example 1 — Take the First Letters

Cut the first five characters of a text with accents.

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

echo iconv_substr($text, 0, 5, "UTF-8"), PHP_EOL;

How It Works

  1. Start at position 0.
  2. Take 5 characters. The é counts as one.
  3. No letter is cut in half.
  4. Print the part. You see Héllo.

Example 2 — Count from the End

A negative offset starts from the end.

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

echo iconv_substr($text, -5, null, "UTF-8"), PHP_EOL;
echo iconv_substr("Привет мир", 7, null, "UTF-8"), PHP_EOL;

How It Works

  1. Use -5. The count starts 5 characters from the end.
  2. Pass null as the length. It means to the end.
  3. Try Cyrillic text. Each letter is 2 bytes but one character.
  4. Start at 7 to get the last word мир.

Example 3 — Shorten a Title

Cut a long title and add three dots.

PHP
<?php
$title = "Ünïcödé tutorial for beginners";

echo iconv_substr($title, 0, 10, "UTF-8") . "...", PHP_EOL;

How It Works

  1. Use a title with accents.
  2. Take the first 10 characters.
  3. Add `. ..` with the dot operator.
  4. Use it for page titles and previews.

Frequently Asked Questions

iconv_substr() returns part of a string by character position, using a given character set.
iconv_substr(string $string, int $offset, ?int $length = null, ?string $encoding = null): string|false.
substr() cuts by bytes and can break a UTF-8 letter. iconv_substr() cuts by characters.
Yes. A negative offset counts from the end of the string.
Yes. mb_substr() does the same job and is built into the mbstring extension.

Next: ob_iconv_handler()

ob_iconv_handler() converts the output buffer to another character set.

ob_iconv_handler() 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