PHP Reference Β· String Functions
PHP iconv() function
Reference for PHP iconv(): convert text between character encodings, //TRANSLIT and //IGNORE suffixes, iconv_strlen and when to prefer mb_convert_encoding.
iconv() converts a string from one character encoding to another, for example from ISO-8859-1 (Latin-1) to UTF-8. It relies on the system's iconv library, so the list of supported encodings and some details of its behavior depend on the platform PHP runs on.
For ordinary UTF-8 work, mb_convert_encoding() is usually the better choice, because its behavior is the same everywhere. iconv() remains useful for its //TRANSLIT option (approximating characters such as Γ© as e), though that output also varies between systems.
<?php $utf8 = iconv("ISO-8859-1", "UTF-8", "caf\xE9"); echo $utf8; // cafΓ©
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
iconv(string $from_encoding, string $to_encoding, string $string): string|false iconv_strlen(string $string, ?string $encoding = null): int|false iconv_substr(string $string, int $offset, ?int $length = null, ?string $encoding = null): string|false iconv_strpos(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): int|false
| Name | Description |
|---|---|
$from_encoding |
The encoding of $string, such as "UTF-8" or "ISO-8859-1". |
$to_encoding |
The target encoding. Can end with //TRANSLIT (approximate unavailable characters) or //IGNORE (skip them). |
$string |
The text to convert. |
Return value: the converted string, or false on failure, with a notice. Failure happens when the input contains bytes that are invalid in $from_encoding, or when a character cannot be represented in $to_encoding and no suffix is given.
Usage notes
- Check for
false. Without//IGNOREor//TRANSLIT, one unrepresentable character makes the whole call fail. //TRANSLITis platform-dependent.iconv("UTF-8", "ASCII//TRANSLIT", "cafΓ©")can givecafe,caf'e(what the runner on this site prints),caf?or an error, depending on the library and the locale. Do not rely on it for URL slugs without testing on your servers.//IGNOREis unreliable in some library versions, which returnfalseanyway for invalid input. Clean data withmb_scrub()first.- Use UTF-8 everywhere you can. Convert at the boundary where legacy data enters (old files, databases, email headers) and keep everything inside your program in UTF-8.
- Replaces the deprecated
utf8_encode()andutf8_decode()for Latin-1; see utf8_encode(). - iconv extension functions also include
iconv_mime_encode()andiconv_mime_decode()for email headers. - Availability. The iconv extension is enabled in most builds but is optional; check with
function_exists("iconv").