PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

iconv_get_encoding> <db2_tables
Last updated: Tue, 27 Nov 2007

view this page in

Funciones iconv

Introducción

Este módulo contiene una interfaz con la facilidad de conversión de juegos de caracteres iconv. Con éste módulo, es posible convertir una cadena representada por un juego de caracteres local a una representada por otro juego de caracteres, que puede ser el juego de caracteres Unicode. Los juegos de caracteres soportados dependen de la implementación de iconv en su sistema. Note que la función iconv en algunos sistemas puede que no funcione como lo espera. En tal caso, sería una buena idea instalar la biblioteca » libiconv GNU. Es muy probable que consiga unos resultados más consistentes.

A partir de PHP 5.0.0, esta extensión viene con varias funciones utilitarias que le ayudan a escribir scripts multi-lingües. Echemos un vistazo a las siguientes secciones para explorar las nuevas características.

Requisitos

No necesita de nada si el sistema que usa es uno de los sistemas recientes compatibles con POSIX, ya que las bibliotecas C estándar que vienen con ellos ofrecen la facilidad iconv. De otro modo, es necesario instalar la biblioteca » libiconv en su sistema.

Instalación

Para usar las funciones de éste módulo, el binario PHP debe ser compilado con la siguiente línea de configuración: --with-iconv[=DIR].

Note: Nota para Usuarios de Windows® Para habilitar éste módulo en un entorno Windows®, necesita colocar un archivo DLL llamado iconv.dll o iconv-1.3.dll (antes de 4.2.1), el cual hace parte del paquete binario PHP/Win32, en un directorio especificado por la variable de entorno PATH o uno de los directorios de sistema de su instalación de Windows®. Este módulo es parte de PHP a partir de PHP 5, por lo tanto iconv.dll y php_iconv.dll no se requieren más.

Configuración en tiempo de ejecución

El comportamiento de estas funciones está afectado por los valores definidos en php.ini.

Opciones de configuración de iconv
Nombre Predeterminado Modificable Cambios
iconv.input_encoding "ISO-8859-1" PHP_INI_ALL Disponible desde PHP 4.0.5.
iconv.output_encoding "ISO-8859-1" PHP_INI_ALL Disponible desde PHP 4.0.5.
iconv.internal_encoding "ISO-8859-1" PHP_INI_ALL Disponible desde PHP 4.0.5.
For further details and definitions of the PHP_INI_* constants, see the Directivas de php.ini.

Warning

Algunos sistemas (como IBM AIX) usan "ISO8859-1" en lugar de "ISO-8859-1", así que este valor debe usado en las opciones de configuración y en los parámetros de función.

Note: La opción de configuración iconv.input_encoding actualmente no es usada en absoluto.

Tipos de recursos

Esta extensión no tiene ningún tipo de recurso definido.

Constantes predefinidas

Desde PHP 4.3.0, es posible identificar, en tiempo de ejecución, qué implementación de iconv es adoptada por esta extensión.

Constantes de iconv
Nombre Tipo Descripción
ICONV_IMPL string El nombre de la implementación
ICONV_VERSION string La versión de la implementación

Note: Escribir scripts que dependan de la implementación con éstas constantes no se recomienda en absoluto.

A partir de PHP 5.0.0, las siguientes constantes se encuentran disponibles también:

Constantes de iconv disponibles desde PHP 5.0.0
Nombre Tipo Descripción
ICONV_MIME_DECODE_STRICT integer Una máscara de bits usada para iconv_mime_decode()
ICONV_MIME_DECODE_CONTINUE_ON_ERROR integer Una máscara de bits usada para iconv_mime_decode()

Ver también

Vea también las funciones GNU Recode.

Table of Contents



iconv_get_encoding> <db2_tables
Last updated: Tue, 27 Nov 2007
 
add a note add a note User Contributed Notes
iconv
dermatin uses web de
26-Oct-2007 10:56
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.
10-Apr-2007 12: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 ("<p>$prefix $suffix");
        if ($prefix==215) {
            $windows1255 .= chr($suffix+80);
            $i++;
        }
        elseif ($prefix==214) {
            $windows1255 .= chr($suffix+16);
            $i++;
        }
        else {
            $windows1255 .= $chars[$i];
        }
    }
    return $windows1255;
}
Erel Segal - Rent a Brain
25-Mar-2007 09:32
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 ("<p>$prefix $suffix");
        if ($prefix==215)
            $windows1255 .= chr($suffix+80);
        elseif ($prefix==214)
            $windows1255 .= chr($suffix+16);
        else
            $windows1255 .= $chars[$i];
    }
    return $windows1255;
}
Fabian Ketchup
13-Sep-2006 04:00
// 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";
nod at mobi dot kz
17-Jul-2006 05:17
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);
}
andrej009
16-Mar-2006 02:22
There's one more special german character: ß (sometimes displayed as Ϋ)

so: case 159: $out .= "ß";break;
08-Nov-2005 10: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);
Christophe Lienert
27-Sep-2005 12:09
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;
}
The Godfather
15-Dec-2004 03:36
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;
}
tokiee at hotmail dot com
19-Aug-2004 11:40
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.
thierry.bo
24-Dec-2003 12:26
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.
14-Sep-2002 03: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.
elk at NOSPAMmodel-fx dot com
25-Jul-2002 06:39
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

iconv_get_encoding> <db2_tables
Last updated: Tue, 27 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites