Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : iconv Functions

iconv Functions

Introduction

This module contains an interface to iconv character set conversion facility. With this module, you can turn a string represented by a local character set into the one represented by another character set, which may be the Unicode character set. Supported character sets depend on the iconv implementation of your system. Note that the iconv function on some systems may not work as you expect. In such case, it'd be a good idea to install the » GNU libiconv library. It will most likely end up with more consistent results.

Since PHP 5.0.0, this extension comes with various utility functions that help you to write multilingual scripts. Let's have a look at the following sections to explore the new features.

Requirements

You will need nothing if the system you are using is one of the recent POSIX-compliant systems because standard C libraries that are supplied in them must provide iconv facility. Otherwise, you have to get the » libiconv library installed in your system.

Installation

To use functions provided by this module, the PHP binary must be built with the following configure line: --with-iconv[=DIR].

Note to Windows® Users:

In order to enable this module on a Windows® environment, you need to put a DLL file named iconv.dll or iconv-1.3.dll (prior to 4.2.1) which is bundled with the PHP/Win32 binary package into a directory specified by the PATH environment variable or one of the system directories of your Windows® installation.

This module is part of PHP as of PHP 5 thus iconv.dll and php_iconv.dll is not needed anymore.

Runtime Configuration

The behaviour of these functions is affected by settings in php.ini.

Table 129. Iconv configuration options

Name Default Changeable Changelog
iconv.input_encoding "ISO-8859-1" PHP_INI_ALL Available since PHP 4.0.5.
iconv.output_encoding "ISO-8859-1" PHP_INI_ALL Available since PHP 4.0.5.
iconv.internal_encoding "ISO-8859-1" PHP_INI_ALL Available since PHP 4.0.5.


For further details and definitions of the PHP_INI_* constants, see the Appendix I, php.ini directives.

Warning:

Some systems (like IBM AIX) use "ISO8859-1" instead of "ISO-8859-1" so this value has to be used in configuration options and function parameters.

Note:

Configuration option iconv.input_encoding is currently not used for anything.

Resource Types

This extension has no resource types defined.

Predefined Constants

Since PHP 4.3.0 it is possible to identify at runtime which iconv implementation is adopted by this extension.

Table 130. iconv constants

Name Type Description
ICONV_IMPL string The implementation name
ICONV_VERSION string The implementation version


Note:

Writing implementation-dependent scripts with these constants is strongly discouraged.

Since PHP 5.0.0, the following constants are also available:

Table 131. iconv constants available since PHP 5.0.0

Name Type Description
ICONV_MIME_DECODE_STRICT integer A bitmask used for iconv_mime_decode()
ICONV_MIME_DECODE_CONTINUE_ON_ERROR integer A bitmask used for iconv_mime_decode()


See Also

See also GNU Recode functions.

Table of Contents

iconv_get_encoding — Retrieve internal configuration variables of iconv extension
iconv_mime_decode_headers — Decodes multiple MIME header fields at once
iconv_mime_decode — Decodes a MIME header field
iconv_mime_encode — Composes a MIME header field
iconv_set_encoding — Set current setting for character encoding conversion
iconv_strlen — Returns the character count of string
iconv_strpos — Finds position of first occurrence of a needle within a haystack
iconv_strrpos — Finds the last occurrence of a needle within a haystack
iconv_substr — Cut out part of a string
iconv — Convert string to requested character encoding
ob_iconv_handler — Convert character encoding as output buffer handler

Code Examples / Notes » ref.iconv

the godfather

With this function you can translate the german Symbols from the character set UTF-8 in windows-1252.
function convert_text($str){
 $out = '';
 for ($i = 0; $i<strlen($str);$i++){
  $ch = ord($str{$i});
  switch($ch){
        case 252: $out .= chr(129);break; //u Umlaut
        case 220: $out .= chr(154);break;//U Umlaut
        case 228: $out .= chr(132);break;//a Umlaut  
        case 196: $out .= chr(142);break;//A Umlaut
        case 214: $out .= chr(153);break;//O Umlaut  
        case 246: $out .= chr(148);break;//o Umlaug
        case 223: $out .= chr(225);break;//SZ
        default : $out .= chr($ch) ;
  }
 }
 return $out;
}


thierry.bo

Windows users.
Personaly I leaved all php dlls in \php\dlls\ directory, just adding this path to my system path, and iconv.dll supplied with php 4.3.2 works fine, also leaving supplied php_iconv.dll in my \php\extensions\ directory. This was working fine with Apache and Omnihttpd server I use.
As soon I installed IIS on the same server, php complained about not finding php_iconv.dll in the extensions directory. In fact PHP with IIS loads all extensions in my \php\extensions directory correctly, except php_iconv.dll.
Although iconv.dll is in my system path, the only way to load php_iconv.dll was to copy iconv.dll file in \%winnt\system32 directory. With other servers, iconv.dll can be in anywhere in the system path.


elk

To compile libiconv under Slackware 7.0 or 8.0 without errors (either with the apache module of PHP or the CGI version), you must specify the full path of the libiconv installation.
Exemple :
      --with-iconv=/usr/local


andrej009

There's one more special german character: ß (sometimes displayed as Ϋ)
so: case 159: $out .= "ß";break;


dermatin uses web de

there's a little error in the post from andrej009 below.
it must be
case 159: $out .= "ss";break;
because the function converts umlauts to "normal" characters.


erel segal - rent a brain

Note that my mysql_iconv will not translate correctly the Hebrew dotting symbols (Niqqud) - they will be converted into question marks.
Here is a straightforward (and not very efficient) solution:
function utf8_to_windows1255($utf8) {
$windows1255 = "";
$chars = preg_split("//",$utf8);
for ($i=1; $i<count($chars)-1; $i+=2) {
$prefix = ord($chars[$i]);
$suffix = ord($chars[$i+1]);
print ("

$prefix $suffix");
if ($prefix==215)
$windows1255 .= chr($suffix+80);
elseif ($prefix==214)
$windows1255 .= chr($suffix+16);
else
$windows1255 .= $chars[$i];
}
return $windows1255;
}


09-apr-2007 10:43

Just a little fix for the utf8_to_windows1255 function above. The original function doesn't convert English characters well:
function utf8_to_windows1255($utf8) {
   $windows1255 = "";
   $chars = preg_split("//",$utf8);
   for ($i=1; $i<count($chars)-1; $i++) {
       $prefix = ord($chars[$i]);
       $suffix = ord($chars[$i+1]);
       //print ("

$prefix $suffix");
       if ($prefix==215) {
           $windows1255 .= chr($suffix+80);
           $i++;
       }
       elseif ($prefix==214) {
           $windows1255 .= chr($suffix+16);
           $i++;
       }
       else {
           $windows1255 .= $chars[$i];
       }
   }
   return $windows1255;
}


christophe lienert

In addition to Godfather's note below, you may find this function useful just as well.
// function to change german umlauts into ue, oe, etc.
function cv_input($str){
  $out = "";
  for ($i = 0; $i<strlen($str);$i++){
  $ch= ord($str{$i});
  switch($ch){
case 195: $out .= "";break;
case 164: $out .= "ae"; break;
case 188: $out .= "ue"; break;
case 182: $out .= "oe"; break;
case 132: $out .= "Ae"; break;
case 156: $out .= "Ue"; break;
case 150: $out .= "Oe"; break;
default : $out .= chr($ch) ;
  }
  }
  return $out;
}


nod

If you need convert string from Windows-1251 to 866. Some characters of 1251 haven't representation on DOS 866. For example, long dash -- chr(150) will be converted to 0, after that iconv finish his work and other charactes  will be skiped. Problem characters range in win1251 (128-159,163,165-167,169,171-174,177-182,187-190).
Use this:
//$text  -  input text in windows-1251
//$cout  -  output text in 866 (cp866, dos ru ascii)
for($i=0;$i<strlen($text);$i++) {
$ord=ord($text[$i]);
if($ord>=192&&$ord<=239) $cout.=chr($ord-64);
elseif($ord>=240&&$ord<=255) $cout.=chr($ord-16);
elseif($ord==168) $cout.=chr(240);
elseif($ord==184) $cout.=chr(241);
elseif($ord==185) $cout.=chr(252);
elseif($ord==150||$ord==151) $cout.=chr(45);
elseif($ord==147||$ord==148||$ord==171||$ord==187) $cout.=chr(34);
elseif($ord>=128&&$ord<=190) $i=$i; //нет представления данному символу
else $cout.=chr($ord);
}


tokiee

iconv now has been built-in, at least in PHP >= 5.0.1 for win32. You don't have to modify php.ini for this. Actually you should not. And clearly, libiconv does not need to be installed.

14-sep-2002 01:23

I'm not sure how recent version of
glibc 2.x Slackware 7.x/8.x comes with, but
it's very likely that it comes with glibc 2.2.x.
In that case, you don't have to bother at all to
install libiconv in /usr/local. iconv(3) in glibc 2.2.x
is very good (thanks to Ulrich Drepper and
Bruno Haible. the latter is the author of libiconv).
libiconv is very handy for those outdated/non-standard-compliant Unix
and non-Unix systems that don't have
sufficiently good iconv(3) in their C library.


08-nov-2005 08:05

But this is a very slow method to convert this:
// function to change german umlauts into ue, oe, etc.
function cv_input($str){
Better try this:
$tr = array(chr(xyz) => 'ß', chr(160) => ' '); // Just a simple example, put all your characters in there
$string = strtr($string, $tr);


fabian ketchup

// Simple file translation.
$FileToconvert = "menu.xml";
$FileConverted = "menu2.xml";
echo "Converting $FileToconvert ...";
file_put_contents($FileConverted, iconv("ISO-8859-1","UTF-8",file_get_contents($FileToconvert)));
echo "File converted in $FileConverted";


Change Language


Follow Navioo On Twitter
.NET Functions
Apache-specific Functions
Alternative PHP Cache
Advanced PHP debugger
Array Functions
Aspell functions [deprecated]
BBCode Functions
BCMath Arbitrary Precision Mathematics Functions
PHP bytecode Compiler
Bzip2 Compression Functions
Calendar Functions
CCVS API Functions [deprecated]
Class/Object Functions
Classkit Functions
ClibPDF Functions [deprecated]
COM and .Net (Windows)
Crack Functions
Character Type Functions
CURL
Cybercash Payment Functions
Credit Mutuel CyberMUT functions
Cyrus IMAP administration Functions
Date and Time Functions
DB++ Functions
Database (dbm-style) Abstraction Layer Functions
dBase Functions
DBM Functions [deprecated]
dbx Functions
Direct IO Functions
Directory Functions
DOM Functions
DOM XML Functions
enchant Functions
Error Handling and Logging Functions
Exif Functions
Expect Functions
File Alteration Monitor Functions
Forms Data Format Functions
Fileinfo Functions
filePro Functions
Filesystem Functions
Filter Functions
Firebird/InterBase Functions
Firebird/Interbase Functions (PDO_FIREBIRD)
FriBiDi Functions
FrontBase Functions
FTP Functions
Function Handling Functions
GeoIP Functions
Gettext Functions
GMP Functions
gnupg Functions
Net_Gopher
Haru PDF Functions
hash Functions
HTTP
Hyperwave Functions
Hyperwave API Functions
i18n Functions
IBM Functions (PDO_IBM)
IBM DB2
iconv Functions
ID3 Functions
IIS Administration Functions
Image Functions
Imagick Image Library
IMAP
Informix Functions
Informix Functions (PDO_INFORMIX)
Ingres II Functions
IRC Gateway Functions
PHP / Java Integration
JSON Functions
KADM5
LDAP Functions
libxml Functions
Lotus Notes Functions
LZF Functions
Mail Functions
Mailparse Functions
Mathematical Functions
MaxDB PHP Extension
MCAL Functions
Mcrypt Encryption Functions
MCVE (Monetra) Payment Functions
Memcache Functions
Mhash Functions
Mimetype Functions
Ming functions for Flash
Miscellaneous Functions
mnoGoSearch Functions
Microsoft SQL Server Functions
Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
Mohawk Software Session Handler Functions
mSQL Functions
Multibyte String Functions
muscat Functions
MySQL Functions
MySQL Functions (PDO_MYSQL)
MySQL Improved Extension
Ncurses Terminal Screen Control Functions
Network Functions
Newt Functions
NSAPI-specific Functions
Object Aggregation/Composition Functions
Object property and method call overloading
Oracle Functions
ODBC Functions (Unified)
ODBC and DB2 Functions (PDO_ODBC)
oggvorbis
OpenAL Audio Bindings
OpenSSL Functions
Oracle Functions [deprecated]
Oracle Functions (PDO_OCI)
Output Control Functions
Ovrimos SQL Functions
Paradox File Access
Parsekit Functions
Process Control Functions
Regular Expression Functions (Perl-Compatible)
PDF Functions
PDO Functions
Phar archive stream and classes
PHP Options&Information
POSIX Functions
Regular Expression Functions (POSIX Extended)
PostgreSQL Functions
PostgreSQL Functions (PDO_PGSQL)
Printer Functions
Program Execution Functions
PostScript document creation
Pspell Functions
qtdom Functions
Radius
Rar Functions
GNU Readline
GNU Recode Functions
RPM Header Reading Functions
runkit Functions
SAM - Simple Asynchronous Messaging
Satellite CORBA client extension [deprecated]
SCA Functions
SDO Functions
SDO XML Data Access Service Functions
SDO Relational Data Access Service Functions
Semaphore
SESAM Database Functions
PostgreSQL Session Save Handler
Session Handling Functions
Shared Memory Functions
SimpleXML functions
SNMP Functions
SOAP Functions
Socket Functions
Standard PHP Library (SPL) Functions
SQLite Functions
SQLite Functions (PDO_SQLITE)
Secure Shell2 Functions
Statistics Functions
Stream Functions
String Functions
Subversion Functions
Shockwave Flash Functions
Swish Functions
Sybase Functions
TCP Wrappers Functions
Tidy Functions
Tokenizer Functions
Unicode Functions
URL Functions
Variable Handling Functions
Verisign Payflow Pro Functions
vpopmail Functions
W32api Functions
WDDX Functions
win32ps Functions
win32service Functions
xattr Functions
xdiff Functions
XML Parser Functions
XML-RPC Functions
XMLReader functions
XMLWriter Functions
XSL functions
XSLT Functions
YAZ Functions
YP/NIS Functions
Zip File Functions
Zlib Compression Functions
eXTReMe Tracker