• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • iconv_mime_decode_headers()

    (PHP 5, PHP 7)

    一次性解码多个MIME头字段

    说明

    iconv_mime_decode_headers(string $encoded_headers[,int $mode= 0[,string $charset= ini_get("iconv.internal_encoding")]]): array

    一次性解码多个MIME头字段。

    参数

    $encoded_headers

    编码过的头,是一个字符串。

    $mode

    $mode决定了iconv_mime_decode_headers()遇到畸形MIME头字段时的行为。你可以指定为以下位掩码的任意组合。

    iconv_mime_decode_headers()接受的位掩码
    常量描述
    1ICONV_MIME_DECODE_STRICT如果设置了,给定的头将会以» RFC2047定义的标准完全一致。这个选项默认禁用,因为大量有问题的邮件用户代理不遵循标准并产生不正确的MIME头。
    2ICONV_MIME_DECODE_CONTINUE_ON_ERROR如果设置了,iconv_mime_decode_headers()尝试忽略任何语法错误并继续处理指定的头。
    $charset

    可选参数$charset指定了字符集结果的表现。如果省略了,将使用iconv.internal_encoding。

    返回值

    成功时返回$encoded_headers指定的MIME头的整套关联数组,解码时出现错误则返回FALSE

    返回元素的每个键代表独立字段名,相应的元素代表一个字段值。如果有多个同一名称的字段,iconv_mime_decode_headers()自动将他们按出现顺序结合成数字索引的数组。

    范例

    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
            )
    )
    

    参见

    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);
    ?>