解析MIME消息时出现奇怪的递归错误

时间:2011-12-03 13:15:36

标签: php email recursion mime-types

好的我要管理员我不是递归的主人。这可能是一个新手问题。

我要做的是计算部分编号。正如您所看到的那样,当您深入了解时,部分编号会以可预测的方式更改

multipart/mixed
 -- (part 0, sec. "1") multipart/related
    -- (part 0, sec. "1.1") multipart/alternative
       -- (part 0, sec. "1.1.1") text/plain
       -- (part 1, sec. "1.1.2") text/html
    -- (part 1, sec. "1.2") image/gif
 -- (part 1, sec. "2") image/png

也就是说,节号只是零件号(加1)后跟一个点,再由零件号(加1),多少次取决于嵌套级别。

传递给$struct函数的简单parse()就像:

object(stdClass)
   public 'type' => int // if 1 it's multipart
   public 'parts' => array // Inner parts

虽然我的功能很像这样:

public function parse($struct, $depth = '')
{
   if(!isset($struct->parts)) return; // Base case of recursion: no parts inside

   // $struct->parts is array: index starting from 0.
   for($i = 0, $j = count($struct->parts); $i < $j; $i++)
   {
      $part = $struct->parts[$i]; // Current part
      $ptno = $i + 1; // This is the part number, will be used to build $secno

      // Multipart? Go further in recursion passing the new level of nesting
      if($part->type == 1) $this->parse($part, $depth .= "$partno" . ".");

      // Compute the section number with the given $depth (if any)
      // ACTUALLY NOT WORKING
      $secno = !empty($depth) ? "$depth$ptno" : "$ptno";

      // Where am i?
      echo self::$TYPES[$part->type] . '/' . $part->subtype . ": $secno<br/>";
   }
}

输出(错误):

text/PLAIN: 1.1.1
text/HTML: 1.1.2
multipart/ALTERNATIVE: 1.1.1
image/GIF: 1.1.2
multipart/RELATED: 1.1
image/PNG: 1.2

应该如何

text/PLAIN: 1.1.1
text/HTML: 1.1.2
multipart/ALTERNATIVE: 1.1
image/GIF: 1.2
multipart/RELATED: 1
image/PNG: 2

编辑:copy&amp;粘贴测试数据:

$test = (object) array(
    'type' => 1, // multipart
    'subtype' => 'MIXED',
    'parts' => array(
        (object) array(
            'type'    => 1, // multipart
            'subtype' => 'RELATED',
            'parts'   => array(
                (object) array(
                    'type'    => 1, // multipart
                    'subtype' => 'ALTERNATIVE',
                    'parts'   => array(
                        (object) array('type' => 0, 'subtype' => 'PLAIN'),
                        (object) array('type' => 0, 'subtype' => 'HTML'),
                    )
                ),
                (object) array(
                    'type'    => 5, // image
                    'subtype' => 'GIF'
                )
            )
        ),
        (object) array(
            'type'    => 5, // image
            'subtype' => 'PNG'
        )
    )
);

2 个答案:

答案 0 :(得分:2)

问题是当你.=追加$partno时,你.追加$this->parse($part, $depth .= "$partno" . ".");

public function parse($struct, $depth = '')
{
   if(!isset($struct->parts)) return; // Base case of recursion: no parts inside

   // $struct->parts is array: index starting from 0.
   for($i = 0, $j = count($struct->parts); $i < $j; $i++)
   {
      $part = $struct->parts[$i]; // Current part
      $ptno = $i + 1; // This is the part number, will be used to build $secno

      // Multipart? Go further in recursion passing the new level of nesting
      if($part->type == 1) $this->parse($part, $depth . "$partno" . ".");

      // Compute the section number with the given $depth (if any)
      $secno = !empty($depth) ? "$depth$ptno" : "$ptno";

      // Where am i?
      echo self::$TYPES[$part->type] . '/' . $part->subtype . ": $secno<br/>";
   }
}

因此,在代码的上下文中修复它:

public function parse($struct, $depth = '')
{
   if(!isset($struct->parts)) return; // Base case of recursion: no parts inside

   // $struct->parts is array: index starting from 0.
   for($i = 0, $j = count($struct->parts); $i < $j; $i++)
   {
      $part = $struct->parts[$i]; // Current part
      $ptno = $i + 1; // This is the part number, will be used to build $secno

      // Compute the section number with the given $depth (if any)
      $secno = $depth . $ptno

      // Multipart? Go further in recursion passing the new level of nesting
      if($part->type == 1) $this->parse($part, $secno . '.');

      // Where am i?
      echo self::$TYPES[$part->type] . '/' . $part->subtype . ": $secno<br/>";
   }
}

您可以像这样重构代码,以避免代码重复:

{{1}}

答案 1 :(得分:1)

您的递归电话

$this->parse($part, $depth .= "$partno" . ".");

应该阅读

$this->parse($part, $depth . "$partno" . ".");

代替。否则,您正在更改变量$depth