【发布时间】:2014-01-11 18:21:16
【问题描述】:
我有一个 PHP 文件,我想提取一个带有 div id 的属性值。
代码如下:
<div id="vids" channelname="test">
<?php
load_video(1,$channelname); //channelname mays contains "test"
function load_video($start_index,$channelname)
{
...
}
?>
</div>
我想在调用 (load_video) 的 PHP 函数之前提取 channelname 的值以使用 $channelname。
我试过这个:
$doc = new DOMDocument();
$doc->load('test.php'); //Same result with loadHtml() and load HtmlFile()
$element = $doc->getElementById('vids');
$attr = $doc->getAttribute('channelname');
但它不起作用。
print_r($element); // No results
$element->getAttribute('channelname'); //Fatal error: Call to a member function getAttribute() on a non-object
【问题讨论】:
-
我认为您需要通过 eval() 执行您的 php 文件,然后您可以通过 dom 加载生成生成的 html 文件。你能添加完整的代码 eof test.php 吗?
-
你能把 print_r($element); 的输出在你的问题?
-
试试
$element->getAttribute('channelname'); -
$element->getAttribute('channelname');返回 getAttribute() 函数的错误
标签: php html getattribute