質問

I have a PHP file, and I want to extract an attribute value with a div id.

Here the code:

<div id="vids" channelname="test">
<?php
 load_video(1,$channelname); //channelname mays contains "test"
 function load_video($start_index,$channelname)
 {
  ...
 }
 ?>
</div>

I would like to extract the value of channelname before the PHP function called (load_video) to use $channelname.

I tried this:

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

But it doesn't work.

print_r($element); // No results
$element->getAttribute('channelname'); //Fatal error: Call to a member function getAttribute() on a non-object
役に立ちましたか?

解決

Try this:

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');
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top