【问题标题】:getAttribute in a php/Html filephp/Html 文件中的 getAttribute
【发布时间】: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-&gt;getAttribute('channelname');
  • $element->getAttribute('channelname');返回 getAttribute() 函数的错误

标签: php html getattribute


【解决方案1】:

试试这个:

ob_start();
include 'test.php';
$strResult = ob_get_clean();

$doc = new DOMDocument();
$doc->loadHTML($strResult); //Same result with loadHtml() and load HtmlFile()
$element = $doc->getElementById('vids');
$attr = $element->getAttribute('channelname');

【讨论】:

  • @user3185672 :我已经在本地进行了测试。它应该工作。你可以试试这个,如果有任何错误,请告诉我?
  • 它对我不起作用:致命错误:达到“100”的最大函数嵌套级别,正在中止!在第 4 行 ob_start();
  • 增加 php.ini 中 xdebug.max_nesting_level 的值
  • 我想我理解这个问题。事实上
    和 PHP 函数在同一个文件 (test.php) 中。如果我创建一个新文件,它也可以工作,但我不能用它做我想做的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
  • 1970-01-01
  • 2012-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多