iconv_mime_decode_headers
(PHP 5, PHP 7, PHP 8)
iconv_mime_decode_headers — 一次性解码多个 MIME
头字段
说明
一次性解码多个 MIME
头字段。
参数
headers
-
编码过的头,是一个字符串。
mode
-
mode
决定了 iconv_mime_decode_headers() 遇到畸形MIME
头字段时的行为。 你可以指定为以下位掩码的任意组合。iconv_mime_decode_headers() 接受的位掩码 值 常量 描述 1 ICONV_MIME_DECODE_STRICT 如果设置了,给定的头将会以 » RFC2047 定义的标准完全一致。 这个选项默认禁用,因为大量有问题的邮件用户代理不遵循标准并产生不正确的 MIME
头。2 ICONV_MIME_DECODE_CONTINUE_ON_ERROR 如果设置了,iconv_mime_decode_headers() 尝试忽略任何语法错误并继续处理指定的头。 encoding
-
可选参数
encoding
指定了字符集结果的表现。 如果省略或为null
,将使用 iconv.internal_encoding。
返回值
成功时返回 headers
指定的 MIME
头的整套关联数组,解码时出现错误则返回 false
。
返回元素的每个键代表独立字段名,相应的元素代表一个字段值。如果有多个同一名称的字段,iconv_mime_decode_headers() 自动将他们按出现顺序结合成数字索引的数组。请注意,标头名称不区分大小写。
更新日志
版本 | 说明 |
---|---|
8.0.0 |
encoding 现在可为 null。
|
示例
示例 #1 iconv_mime_decode_headers() 示例
<?php
$headers_string = <<<EOF
Subject: =?UTF-8?B?UHLDvGZ1bmcgUHLDvGZ1bmc=?=
To: example@example.com
Date: Thu, 1 Jan 1970 00:00:00 +0000
Message-Id: <example@example.com>
Received: from localhost (localhost [127.0.0.1]) by localhost
with SMTP id example for <example@example.com>;
Thu, 1 Jan 1970 00:00:00 +0000 (UTC)
(envelope-from example-return-0000-example=example.com@example.com)
Received: (qmail 0 invoked by uid 65534); 1 Thu 2003 00:00:00 +0000
EOF;
$headers = iconv_mime_decode_headers($headers_string, 0, "ISO-8859-1");
print_r($headers);
?>
以上示例会输出:
Array ( [Subject] => Prüfung Prüfung [To] => example@example.com [Date] => Thu, 1 Jan 1970 00:00:00 +0000 [Message-Id] => <example@example.com> [Received] => Array ( [0] => from localhost (localhost [127.0.0.1]) by localhost with SMTP id example for <example@example.com>; Thu, 1 Jan 1970 00:00:00 +0000 (UTC) (envelope-from example-return-0000-example=example.com@example.com) [1] => (qmail 0 invoked by uid 65534); 1 Thu 2003 00:00:00 +0000 ) )
参见
- iconv_mime_decode() - 解码一个MIME头字段
- mb_decode_mimeheader() - 解码 MIME 头字段中的字符串
- imap_mime_header_decode() - Decode MIME header elements
- imap_base64() - Decode BASE64 encoded text
- imap_qprint() - Convert a quoted-printable string to an 8 bit string
+添加备注
用户贡献的备注 2 notes
phpmanual at NOSPAM dot headbank dot co dot uk ¶
2 months ago
Just in case this catches anyone else: If your headers string has any leading linebreaks, this function will reject it and return an empty array. If that might apply to your input, sanitise it with ltrim().
Trailing empty lines are tolerated/ignored.
Other quirks I noticed just now:
1. Leading whitespace (" " or "\t") in the *first* line will be included in the header's key name in the returned array. ltrim() will prevent that too.
2. Leading whitespace in any subsequent header (before the key) will cause that line to be appended to the preceding header's value, as though it were a run-on of that header.
TheConstructor ¶
14 years ago
If you need lower-case header-names (as I read the documentation case is not guranteed) try something like
<?php
$headers_string = <<<EOF
Subject: =?UTF-8?B?UHLDvGZ1bmcgUHLDvGZ1bmc=?=
To: example@example.com
Date: Thu, 1 Jan 1970 00:00:00 +0000
Message-Id: <example@example.com>
Received: from localhost (localhost [127.0.0.1]) by localhost
with SMTP id example for <example@example.com>;
Thu, 1 Jan 1970 00:00:00 +0000 (UTC)
(envelope-from example-return-0000-example=example.com@example.com)
Received: (qmail 0 invoked by uid 65534); 1 Thu 2003 00:00:00 +0000
EOF;
$headers = iconv_mime_decode_headers($headers_string, 0, "ISO-8859-1");
$headers = array_combine(array_map("strtolower", array_keys($headers)), array_values($headers));
print_r($headers);
?>
备份地址:http://www.lvesu.com/blog/php/function.iconv-mime-decode-headers.php