iconv
(PHP 4 >= 4.0.5, PHP 5, PHP 7)
iconv — Convert string to requested character encoding
Description
$in_charset
, string $out_charset
, string $str
) : string
Performs a character set conversion on the string
str
from in_charset
to out_charset
.
Parameters
-
in_charset
-
The input charset.
-
out_charset
-
The output charset.
If you append the string //TRANSLIT to
out_charset
transliteration is activated. This means that when a character can't be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise,E_NOTICE
is generated and the function will returnFALSE
.CautionIf and how //TRANSLIT works exactly depends on the system's iconv() implementation (cf.
ICONV_IMPL
). Some implementations are known to ignore //TRANSLIT, so the conversion is likely to fail for characters which are illegal for theout_charset
. -
str
-
The string to be converted.
Changelog
Version | Description |
---|---|
5.4.0 |
Since this version, the function returns FALSE on illegal characters,
unless //IGNORE is specified in output charset.
Before, it returned partial output string.
|
Examples
Example #1 iconv() example
<?php
$text = "This is the Euro symbol '€'.";
echo 'Original : ', $text, PHP_EOL;
echo 'TRANSLIT : ', iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text), PHP_EOL;
echo 'IGNORE : ', iconv("UTF-8", "ISO-8859-1//IGNORE", $text), PHP_EOL;
echo 'Plain : ', iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL;
?>
The above example will output something similar to:
Original : This is the Euro symbol '€'. TRANSLIT : This is the Euro symbol 'EUR'. IGNORE : This is the Euro symbol ''. Plain : Notice: iconv(): Detected an illegal character in input string in .\iconv-example.php on line 7
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.iconv.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.