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.
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.
Syntax
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");
Reference
Core Ideas
What to remember when you use iconv:
Idea
Example
Remember
Convert
iconv("ISO-8859-1", "UTF-8", $s)
First the encoding you have, then the one you want.
Close matches
iconv("UTF-8", "ASCII//TRANSLIT", $s)
Swaps é for e. Results depend on the system library.
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.