【发布时间】:2017-04-12 02:07:53
【问题描述】:
我正在开发一个小型模板引擎,并且正在使用 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;
}
我让它按照我想要的方式工作,它抓取页面上的每个 或 标签: 我在这里做错了什么? :) 谢谢this is some rendered PHP! Main column of content
【问题讨论】:
标签: php