Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: iconv 関数 - Manual
[go: Go Back, main page]

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" width="11" height="7"/> <icap_store_event
Last updated: Thu, 31 May 2007

view this page in

LXII. iconv 関数

導入

このモジュールには、iconv による文字集合の変換機能へのインターフェースが含まれています。 このモジュールにより、ローカルな文字集合で表された文字列を、Unicode 文字集合のような 他の文字集合で表わされた文字列に変換することができます。 サポートされる文字集合は、使用するシステムの iconv の実装に依存します。 いくつかのシステムでは iconv 関数は意図した通りに動作しない可能性があることに注意してください。 この場合は、» GNU libiconv ライブラリをインストール すると良いでしょう。 このライブラリの出力は、多くの場合、より妥当なものとなります。

PHP 5.0.0以降、この拡張モジュールには、 多言語スクリプトを書く際に有用な種々のユーティリティが附属しています。 この新機能については、以下のセクションを参照してください。

要件

使用しているシステムが、最近のPOSIX対応システムのどれかである場合には、標準C 言語ライブラリの中にiconv機能が含まれているため、何かをする必要はありません。 そうでない場合は、 » libiconvライブラリを入手して使用する システムにインストールする必要があります。

インストール手順

この関数を使用するには、 PHPバイナリを以下のオプションを指定して 構築する必要があります。 --with-iconv[=DIR]

Windows® ユーザへの注意: Windows®環境でこのモジュールを有効にするには、 PHP/Win32バイナリパッケージに附属するiconv.dllまたは iconv-1.3.dll (4.2.1より前)という名前の DLLファイルを環境変数PATHで指定した ディレクトリまたは Windows® のシステムディレクトリにコピーする 必要があります。

このモジュールは、PHP 5 で PHP の一部として組み込まれました。 iconv.dllphp_iconv.dll は もはや必要ありません。

実行時設定

php.ini の設定により動作が変化します。

表 127. Iconv 設定オプション

名前デフォルト変更の可否変更履歴
iconv.input_encoding"ISO-8859-1"PHP_INI_ALLPHP 4.0.5 以降で使用可能です。
iconv.output_encoding"ISO-8859-1"PHP_INI_ALLPHP 4.0.5 以降で使用可能です。
iconv.internal_encoding"ISO-8859-1"PHP_INI_ALLPHP 4.0.5 以降で使用可能です。

PHP_INI_* 定数の詳細および定義については 付録 I. php.ini ディレクティブ を参照してください。

警告

(IBM AIX のように)いくつかのシステムでは "ISO-8859-1" ではなく "ISO8859-1" を使用することがあります。そのような場合は、設定オプションや 関数のパラメータにはこの値を使用しなければなりません。

注意: 設定オプション iconv.input_encoding は、 今のところ一切利用されていません。

リソース型

リソース型は定義されていません。

定義済み定数

PHP 4.3.0以降、この拡張モジュールで使用される iconv 実装の種類を実行時に調べることができます。

表 128. iconv定数

名前説明
ICONV_IMPLstring実装の名前
ICONV_VERSIONstring実装のバージョン

注意: これらの定数を用いて実装に依存するスクリプトを書くことは全く推奨されません。

PHP 5.0.0以降、以下の定数も利用可能です。

表 129. PHP 5.0.0以降で利用可能なiconv定数

名前説明
ICONV_MIME_DECODE_STRICTintegericonv_mime_decode()で使用されるビットマスク
ICONV_MIME_DECODE_CONTINUE_ON_ERRORintegericonv_mime_decode()で使用されるビットマスク

参考

GNU Recode 関数も参照してください。

目次

iconv_get_encoding — iconv 拡張モジュールの内部設定変数を取得する
iconv_mime_decode_headers — 複数の MIME ヘッダフィールドを一度にデコードする
iconv_mime_decodeMIME ヘッダフィールドをデコードする
iconv_mime_encodeMIME ヘッダフィールドを作成する
iconv_set_encoding — 文字エンコーディング変換用の設定を行なう
iconv_strlen — 文字列の文字数を返す
iconv_strpos — 文字列が最初に現れる場所を見つける
iconv_strrpos — 文字列が最後に現れる場所を見つける
iconv_substr — 文字列の一部を切り出す
iconv — 文字列を指定した文字エンコーディングに変換する
ob_iconv_handler — 出力バッファハンドラとして文字エンコーディングを変換する


add a note add a note User Contributed Notes
iconv 関数
10-Apr-2007 06: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
26-Mar-2007 04: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;
}
Erel Segal - Rent a Brain
10-Dec-2006 08:05
If you don't have iconv, you can use MySQL to do the conversion.

Note that to do this you have to use MySQL's charset names, which are different than iconv names (e.g. "utf8" instead of "utf-8").

<?php
/**
 * @param $string
 * @param $from [string] The name of the current character set of $string, in MySQL format (e.g. "utf8", "hebrew").
 * @param $to [string] The name of the character set to which you want to convert $string.
 * @author Erel Segal - Rent a Brain (http://tora.us.fm/rentabrain)
 * @date 2006-12-10
 */
function mysql_iconv($string, $from, $to) {
   
// keep current character set values:
   
$character_set_database = mysql_result(mysql_query("SELECT @@character_set_client"),0,0);
   
$character_set_results = mysql_result(mysql_query("SELECT @@character_set_results"),0,0);

   
mysql_query("SET character_set_client=$from");
   
mysql_query("SET character_set_results=$to");

   
$string_escaped = mysql_real_escape_string($string);
   
$converted_string = mysql_result(mysql_query("SELECT '$string_escaped'"),0,0);

   
// restore previous character set values:
   
mysql_query("SET character_set_client=$character_set_database");
   
mysql_query("SET character_set_results=$character_set_results");

    return
$converted_string;
}

?>
Fabian Ketchup
13-Sep-2006 10: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 11: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 09:22
There's one more special german character: ß (sometimes displayed as Ϋ)

so: case 159: $out .= "ß";break;
08-Nov-2005 05: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 06: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 10: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 05: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 07: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.
ALecFFer
07-Nov-2003 09:10
To windows users:

Download here iconv version 1.9.1:
http://www.zlatkovic.com/pub/libxml/iconv-1.9.1.win32.zip
14-Sep-2002 09: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
27-Jul-2002 01:07
If you use the libiconv library instead of the libc's iconv support, don't forget to use libiconv() instead of iconv()
elk at NOSPAMmodel-fx dot com
26-Jul-2002 12: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

 
show source | credits | sitemap | contact | advertising | mirror sites