我正在开发一个小型模板引擎,使用DOMDocument解析页面。目前我的测试页面如下:
<block name="content">
<?php echo 'this is some rendered PHP! <br />' ?>
<p>Main column of <span>content</span></p>
</block>
我的课程的一部分看起来像这样:
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;
}
我已经让它按照我的意愿工作了,即它会抓取页面上的每个<block>并将其内容注入到我的模板中,但是它会剥离<block>内部的HTML标签,从而返回以下结果,不包含<p>或<span>标签:
this is some rendered PHP! Main column of content
我在这里做错了什么?:) 谢谢