PHP – 使用 iconv_mime_encode() 函数编写 MIME 标头字段
phpserver side programmingprogramming更新于 2025/7/22 8:07:17
在 PHP 中,iconv_mime_encode() 函数用于编写 MIME 标头字段。这是一个内置的 PHP 函数。
语法
string iconv_mime_encode(string $field_name, string $field_value, array $options=[])
iconv_mime_encode() 函数用于组合并返回一个表示有效 MIME 标头字段的字符串,该字符串如下所示 -
Subject: =ISO-8859-1?Q?Pr=FCfung_f=FFCr?= 由 einer MIME kopfzeile 编写
注意− 在上面的示例中,Subject - 是字段名称,以"=ISO-8859-1?..." 开头的部分是字段值。
参数
iconv_mime_encode() 接受三个不同的参数 − $field_name、$field_value 和 $options。
$field_name − 此参数用于字段名称。
$field_value − 此参数用于字段值。
$options − 使用此参数,您可以通过为可选参数指定一个包含配置项的关联数组来控制 iconv_mime_encode() 的行为。
以下是列表iconv_mime_encode()
支持的配置项项目 | 类型 | 描述 | 默认值 | 示例 |
|---|---|---|---|---|
| scheme | string | The scheme specifies the method to encode a field value by. This item value may be either B (base64) or Q(quoted-printable) encoding scheme. | ||
| input-charset | string | It specifies the character set, the field_name is the first parameter and field_value is the second parameter. If these parameters are not given then the iconv_mime_encode() function assumes that it may be presented in the iconv.internal_charset ini setting. | iconv.internal_charset | ISO-8859-1 |
| output-charset | string | It specifies the character set to use to compose the MIME header. If it is not given then it will use the input-charset value. | input_charset is use as a default value | UTF-8 |
| line-length | integer | It specifies the maximum length of the header lines. | 76 | 996 |
| line-break-chars | string | It specifies the sequence of characters to append to each line as an EOL when folding is performed on a long header field. If it is not given then this default to “\r ” (CR LF) | \r |
示例 1 - 使用"Q"quoted-printable 编码方案
<?php // 使用 iconv_mime_encode() 支持的配置项 $options = array( &"input-charset" => &"ISO-8859-2", &"output-charset" => &"UTF-8", &"line-length" => 76, &"line-break-chars" => &"
" ); // 使用 Q quoted-printable 编码方案 $options["scheme"] = "Q"; // 以下代码将显示结果为 // "Subject: =?UTF-8?Q?Pr=C3=BCfung=20Pr=C3=BCfung?=" echo iconv_mime_encode("Subject", "Prüfung Prüfung", $options); ?>
输出
Subject: =?UTF-8?Q?Pr=C3=83=C2=BCfung=20Pr=C3=83=C2=BCfung?=
示例 2
<?php // 使用 iconv_mime_encode() 支持的配置项 $options = array( &"input-charset" => &"ISO-8859-1", &"output-charset" => &"UTF-8", &"line-length" => 76, &"line-break-chars" => &"
" ); // 使用 base64 编码方案 $options["scheme"] = "B"; // 以下代码将显示结果如下 //"Subject: =?UTF-8?B?UHJlw4PCp29zIE9sw4PCoC50eHQ=?=" echo iconv_mime_encode("Subject", "Preços Olà.txt", $options); ?>
输出
Subject: =?UTF-8?B?UHJlw4PCp29zIE9sw4PCoC50eHQ=?=

