PHP iconv

Beginner
7 min read
Updated: Nov 2026
3 examples
Convert & measure text

Definition and Usage

The PHP iconv extension converts text from one character set to another, such as ISO-8859-1 to UTF-8. It also counts, searches and cuts text by character, and works with email header encodings.

The main function is iconv(). The helpers iconv_strlen(), iconv_substr() and iconv_strpos() count and cut by character instead of by byte, and iconv_mime_decode() reads encoded email headers.

This page explains the ideas. The iconv Functions index lists all 11 function guides.

Know your source encoding.

iconv() needs to know the encoding you have. If you guess wrong, letters turn into garbage or the call returns false. For many sites, the answer is simply to save everything as UTF-8.

PHP Syntax

The usual iconv calls:

PHP
<?php
// Convert text: from, to, string
$utf8 = iconv("ISO-8859-1", "UTF-8", $latin1);

// Count and cut by character
$length = iconv_strlen($utf8, "UTF-8");
$part   = iconv_substr($utf8, 0, 3, "UTF-8");

// Decode an email header field
$subject = iconv_mime_decode($header, 0, "UTF-8");

Core Ideas

What to remember when you use iconv:

IdeaExampleRemember
Converticonv("ISO-8859-1", "UTF-8", $s)First the encoding you have, then the one you want.
Close matchesiconv("UTF-8", "ASCII//TRANSLIT", $s)Swaps é for e. Results depend on the system library.
Counticonv_strlen($s, "UTF-8")Counts characters. strlen() counts bytes.
Cuticonv_substr($s, 0, 5, "UTF-8")Never splits a letter in half.
Searchiconv_strpos($s, "x", 0, "UTF-8")Returns a character position or false.
Email headersiconv_mime_decode($h, 0, 'UTF-8')Turns =?UTF-8?B?...?= into text.

PHP Examples

Three short scripts that show how iconv is used.

Example 1 — Convert Latin-1 to UTF-8

Change the encoding of a short string.

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

$utf8 = iconv("ISO-8859-1", "UTF-8", $latin1);

echo $utf8, PHP_EOL;
iconv() Tutorial

How It Works

  1. The byte \xE9 is the letter é in Latin-1.
  2. iconv() takes the source encoding first.
  3. The target encoding is the second argument.
  4. The result is valid UTF-8 text.

Example 2 — Count Characters

Count letters, not bytes.

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

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

How It Works

  1. The letter é is 2 bytes in UTF-8.
  2. strlen() returns 6, the number of bytes.
  3. iconv_strlen() returns 5, the number of characters.
  4. Always pass the encoding to be safe.

Example 3 — Decode an Email Subject

Read an encoded header field.

PHP
<?php
$header = "Subject: =?UTF-8?B?UHLDvGZ1bmcgUHLDvGZ1bmc=?=";

echo iconv_mime_decode($header, 0, "UTF-8"), PHP_EOL;

How It Works

  1. The encoded word sits between =? and ?=.
  2. The letter B means base64.
  3. iconv_mime_decode() turns it into text.
  4. Pass UTF-8 so the result is predictable.

Next: PHP iconv Functions Index

After you know the basics, open the function index for all 11 guides.

Start withWhy
iconv()Convert text between character sets
iconv_strlen()Count characters
iconv_substr()Cut part of a string by character
iconv_strpos()Find the first match by character position
iconv_mime_decode()Decode an email header
ob_iconv_handler()Convert the output buffer

Full list: PHP iconv Functions index (/php/iconv/function).

Frequently Asked Questions

iconv is a set of functions that convert text between character sets and work with multibyte text and email header encodings.
Call iconv("ISO-8859-1", "UTF-8", $text). The first argument is the encoding you have and the second is the encoding you want.
Adding //TRANSLIT to the target encoding replaces letters that do not exist there with close matches, such as é with e. Results depend on the system library.
Both convert encodings. mbstring is more popular and works the same on every server, while iconv relies on the system library and supports //TRANSLIT.
It returns false when it meets a character that is invalid in the source encoding or missing in the target encoding.
Practice the examples here, then open the iconv Functions index for all 11 function guides.

Browse every iconv function

Open the functions index for categorized guides with examples and FAQs.

iconv Functions Index →

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