-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathmb-detect-encoding.xml
273 lines (260 loc) · 8.94 KB
/
mb-detect-encoding.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 2c17cef6e71c3d85011319cde128cc4edf89a053 Maintainer: hirokawa Status: ready -->
<!-- CREDITS: takagi,mumumu -->
<refentry xml:id="function.mb-detect-encoding" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>mb_detect_encoding</refname>
<refpurpose>文字エンコーディングを検出する</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type class="union"><type>string</type><type>false</type></type><methodname>mb_detect_encoding</methodname>
<methodparam><type>string</type><parameter>string</parameter></methodparam>
<methodparam choice="opt"><type class="union"><type>array</type><type>string</type><type>null</type></type><parameter>encodings</parameter><initializer>&null;</initializer></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>strict</parameter><initializer>&false;</initializer></methodparam>
</methodsynopsis>
<para>
エンコーディングの候補の一覧から、
文字列 <parameter>string</parameter>
のもっとも可能性が高い文字エンコーディングを検出します。
</para>
<para>
追加の情報なしに、
意図した文字エンコーディングを自動で検出する行為は、
全く役に立ちません。
これは、暗号化された文字列を鍵なしにデコードする行為に似ています。
"Content-Type" HTTP ヘッダのような、
データを転送された際に付いてくる情報や、
保存された文字エンコーディングに関する指示を指定することが常に望ましいです。
</para>
<para>
この関数は、全ての文字列が正しいバイト配列とは限らない
マルチバイト文字列を指定するともっとも役に立ちます。
入力となる文字列に誤ったバイトが含まれていた場合、
そのエンコーディングは採用されず、次のものを試します。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>string</parameter></term>
<listitem>
<para>
調べる対象の文字列。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>encodings</parameter></term>
<listitem>
<para>
文字エンコーディングの一覧を試す順番に指定します。
このリストは、文字列の配列または、
カンマ区切りのリストで指定できます。
</para>
<para>
<parameter>encodings</parameter> が省略された場合、
または &null; の場合、
現在の detect_order
(<link linkend="ini.mbstring.detect-order">
mbstring.detect_order</link> 設定オプション
または <function>mb_detect_order</function> で設定したもの)
を使います。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>strict</parameter></term>
<listitem>
<para>
<parameter>encodings</parameter> で指定された
文字エンコーディングのリストの全てに対して、
不正と判定された場合の振る舞いを指定します。
<parameter>strict</parameter> が &false; の場合、
もっとも近いと判定された文字エンコーディングが返されます。
<parameter>strict</parameter> を &true; にした場合、
&false; が返されます。
</para>
<para>
<parameter>strict</parameter> のデフォルト値は
<link linkend="ini.mbstring.strict-detection">
mbstring.strict_detection</link> 設定オプションで指定できます。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
検出した文字エンコーディングを返します。
指定したエンコーディングの全てに対して、不正と判定された場合は &false; を返します。
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.2.0</entry>
<entry>
<function>mb_detect_encoding</function> は、
以下のテキストでないエンコーディングを返さなくなりました:
<literal>"Base64"</literal>, <literal>"QPrint"</literal>,
<literal>"UUencode"</literal>, <literal>"HTML entities"</literal>,
<literal>"7 bit"</literal>, <literal>"8 bit"</literal>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>mb_detect_encoding</function> の例</title>
<programlisting role="php">
<![CDATA[
<?php
// 現在のdetect_orderで文字エンコーディングを検出
echo mb_detect_encoding($str);
// "auto" は mbstring.language の設定を使って展開されます
echo mb_detect_encoding($str, "auto");
// 文字エンコーディングをカンマ区切りのリストで指定することで、encodings 引数を指定
echo mb_detect_encoding($str, "JIS, eucjp-win, sjis-win");
// encodings 引数を配列で指定
$encodings = [
"ASCII",
"JIS",
"EUC-JP"
];
echo mb_detect_encoding($str, $encodings);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title><parameter>strict</parameter> パラメーターの効果</title>
<programlisting role="php">
<![CDATA[
<?php
// 'áéóú' は ISO-8859-1 でエンコードされています
$str = "\xE1\xE9\xF3\xFA";
// この文字列は、ASCII または UTF-8 的には正しくありませんが、
// UTF-8 がもっとも近いと判定されます
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8'], false));
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8'], true));
// 正しいエンコーディングが見つかった場合、
// strict パラメータを指定しても結果は変わりません
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8', 'ISO-8859-1'], false));
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8', 'ISO-8859-1'], true));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(5) "UTF-8"
bool(false)
string(10) "ISO-8859-1"
string(10) "ISO-8859-1"
]]>
</screen>
</example>
</para>
<para>
場合によっては、同じバイト配列が、
複数の文字エンコーディング的に正しいかもしれませんが、
どの解釈が意図されたものなのかを知ることは不可能です。
たとえば、
バイト配列 "\xC4\xA2" は、
以下のように解釈できます:
</para>
<para>
<simplelist>
<member>
"Ä¢" (U+00C4 LATIN CAPITAL LETTER A WITH DIAERESIS のあとに、U+00A2 CENT SIGN が続いたもの)
これらは、ISO-8859-1, ISO-8859-15, Windows-1252
でエンコードされたものと解釈できます。
</member>
<member>
"ФЂ" (U+0424 CYRILLIC CAPITAL LETTER EF のあとに、 U+0402 CYRILLIC CAPITAL LETTER
DJE が続いたもの)
これは、ISO-8859-5 でエンコードされたものと解釈できます。
</member>
<member>
"Ģ" (U+0122 LATIN CAPITAL LETTER G WITH CEDILLA)
これは、UTF-8 でエンコードされたものと解釈できます。
</member>
</simplelist>
</para>
<para>
<example>
<title>複数のエンコーディングと一致した場合の効果</title>
<programlisting role="php">
<![CDATA[
<?php
$str = "\xC4\xA2";
// 3つ全てのエンコーディングに照らして正しい文字列なので、
// 最初のひとつが返されます。
var_dump(mb_detect_encoding($str, ['UTF-8', 'ISO-8859-1', 'ISO-8859-5']));
var_dump(mb_detect_encoding($str, ['ISO-8859-1', 'ISO-8859-5', 'UTF-8']));
var_dump(mb_detect_encoding($str, ['ISO-8859-5', 'UTF-8', 'ISO-8859-1']));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(5) "UTF-8"
string(10) "ISO-8859-1"
string(10) "ISO-8859-5"
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>mb_detect_order</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->