CakeFest 2024: The Official CakePHP Conference

mb_detect_order

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

mb_detect_order文字エンコーディング検出順序を設定あるいは取得する

説明

mb_detect_order(array|string|null $encoding = null): array|bool

自動文字エンコーディング検出の順番を encoding に設定します。

パラメータ

encoding

encoding は、 配列またはカンマ区切りの文字エンコーディングのリストです。サポートされる文字エンコーディングを参照ください。

encoding が省略された場合、または null の場合は、 現在の文字エンコーディング検出順を配列で返します。

この設定は、mb_detect_encoding() および mb_send_mail() に影響します。

mbstring が現在実装しているのは、 以下のエンコーディングを検出するフィルタです。 以下のエンコーディングにおいて無効なバイトシーケンスがあった場合、 エンコーディング検出は失敗します。

UTF-8, UTF-7, ASCII, EUC-JP,SJIS, eucJP-win, SJIS-win, JIS, ISO-2022-JP

ISO-8859-*の場合、mbstring は常に ISO-8859-* として検出します。

UTF-16, UTF-32, UCS2, UCS4 の場合、 エンコーディング検出は常に失敗します。

戻り値

検出順序を設定する場合は、成功した場合に true を返し、失敗したときに false を返します。

検出順序を取得する場合は、エンコーディングの配列を返します。

変更履歴

バージョン 説明
8.0.0 encoding は、nullable になりました。

例1 mb_detect_order() の例

<?php
/* リストで検出順を設定 */
mb_detect_order("eucjp-win,sjis-win,UTF-8");

/* 配列で検出順を設定 */
$ary[] = "ASCII";
$ary[] = "JIS";
$ary[] = "EUC-JP";
mb_detect_order($ary);

/* 現在の検出順を表示 */
echo implode(", ", mb_detect_order());
?>

例2 無意味な順番の例

; 常に ISO-8859-1 として検出されます
detect_order = ISO-8859-1, UTF-8

; ASCII/UTF-7 の値は UTF-8 として有効なため、常に UTF-8 として検出されます
detect_order = UTF-8, ASCII, UTF-7

参考

  • mb_internal_encoding() - 内部文字エンコーディングを設定あるいは取得する
  • mb_http_input() - HTTP 入力文字エンコーディングを検出する
  • mb_http_output() - HTTP 出力文字エンコーディングを設定あるいは取得する
  • mb_send_mail() - エンコード変換を行ってメールを送信する

add a note

User Contributed Notes 1 note

up
0
Anonymous
1 month ago
Perhaps obvious to most everyone, but the
default filter list was shorter than I expected:
['ASCII','UTF-8'], in that order.

c. 2024, 60% of websites globally declared charset 'UTF-8'
So if you're experimenting with multibyte encodings other than UTF-8, you have to specify your detect_order, choosing from the list of implemented filters.
To Top