我正在构建一个程序,允许用户上传他们用文本编辑器或类似Microsoft Office编写的文件,而不仅仅是复制和粘贴它们 . 他们要上传的文件大多数是.rtf格式 . 这个程序是用php制作的,我不是一个非常高级的php程序员,所以我抓了一个rtf到HTML文件转换器形成一个开源php程序员的情人,它工作正常,文件转换和显示但它伴随着这个注意警告说“警告:array_push()期望参数1是数组,在C:中给出null”,我知道我可能正在使用带有不是数组的变量的数组标签,但我试图检查它和它是一个数组,任何人都可以帮助我 .

这里的代码很长,它是:

class RtfElement
{
    protected function Indent($level)
    {
        for($i = 0; $i < $level * 2; $i++) echo "&nbsp;";
    }
}

class RtfGroup extends RtfElement
{
    public $parent;
    public $children;
    public function __construct()
    {
        $this->parent = null;
        $this->children = array();
    }

    public function GetType()
    {
        // No children?
        if(sizeof($this->children) == 0) return null;
        // First child not a control word?
        $child = $this->children[0];
        if(get_class($child) != "RtfControlWord") return null;
        return $child->word;
    }    

    public function IsDestination()
    {
        // No children?
        if(sizeof($this->children) == 0) return null;
        // First child not a control symbol?
        $child = $this->children[0];
        if(get_class($child) != "RtfControlSymbol") return null;
        return $child->symbol == '*';
    }

    public function dump($level = 0)
    {
        echo "<div>";
        $this->Indent($level);
        echo "{";
        echo "</div>";

        foreach($this->children as $child)
        {
            if(get_class($child) == "RtfGroup")
            {
                if ($child->GetType() == "fonttbl") continue;
                if ($child->GetType() == "colortbl") continue;
                if ($child->GetType() == "stylesheet") continue;
                if ($child->GetType() == "info") continue;
                // Skip any pictures:
                if (substr($child->GetType(), 0, 4) == "pict") continue;
                if ($child->IsDestination()) continue;
            }
            $child->dump($level + 2);
        }

        echo "<div>";
        $this->Indent($level);
        echo "}";
        echo "</div>";
    }
}

class RtfControlWord extends RtfElement
{
    public $word;
    public $parameter;

    public function dump($level)
    { 
        echo "<div style='color:green'>";
        $this->Indent($level);
        echo "WORD {$this->word} ({$this->parameter})";
        echo "</div>";
    }
}

class RtfControlSymbol extends RtfElement
{
    public $symbol;
    public $parameter = 0;

    public function dump($level)
    {
        echo "<div style='color:blue'>";
        $this->Indent($level);
        echo "SYMBOL {$this->symbol} ({$this->parameter})";
        echo "</div>";
    }    
}

class RtfText extends RtfElement
{
    public $text;

    public function dump($level)
    {
        echo "<div style='color:red'>";
        $this->Indent($level);
        echo "TEXT {$this->text}";
        echo "</div>";
    }    
}

class RtfReader
{
    public $root = null;

    protected function GetChar()
    {
        $this->char = $this->rtf[$this->pos++];
    }

    protected function ParseStartGroup()
    {
        // Store state of document on stack.
        $group = new RtfGroup();
        if($this->group != null) $group->parent = $this->group;
        if($this->root == null)
        {
            $this->group = $group;
            $this->root = $group;
        }
        else
        {
            array_push($this->group->children, $group);
            $this->group = $group;
        }
    }

    //and the line 140 is inside this function:
    protected function GetChar()
    {
        $this->char = $this->rtf[$this->pos++];
    }
}

我没有写这里的所有代码,因为它太长了,但是从第一行到第140行 .