PHP DOMDocument stripping HTML tags
12:35 17 Sep 2008

I'm working on a small templating engine, and I'm using DOMDocument to parse the pages. My test page so far looks like this:



   ' ?>

   

Main column of content

And part of my class looks like this:

private function parse($tag, $attr = 'name')
{
    $strict = 0;
    /*** the array to return ***/
    $out = array();
    if($this->totalBlocks() > 0)
    {
        /*** a new dom object ***/
        $dom = new domDocument;
        /*** discard white space ***/
        $dom->preserveWhiteSpace = false;

        /*** load the html into the object ***/
        if($strict==1)
        {
            $dom->loadXML($this->file_contents);
        }
        else
        {
            $dom->loadHTML($this->file_contents);
        }

        /*** the tag by its tag name ***/
        $content = $dom->getElementsByTagname($tag);

        $i = 0;
        foreach ($content as $item)
        {
            /*** add node value to the out array ***/
            $out[$i]['name'] = $item->getAttribute($attr);
            $out[$i]['value'] = $item->nodeValue;
            $i++;
        }
    }

    return $out;
}

I have it working the way I want in that it grabs each on the page and injects it's contents into my template, however, it is stripping the HTML tags within the , thus returning the following without the

or tags:

this is some rendered PHP! Main column of content

What am I doing wrong here? :) Thanks

php