Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Simpledom 使用simpleHTMLDom从XHTML中提取DIV,然后打印出来_Simpledom - Fatal编程技术网

Simpledom 使用simpleHTMLDom从XHTML中提取DIV,然后打印出来

Simpledom 使用simpleHTMLDom从XHTML中提取DIV,然后打印出来,simpledom,Simpledom,任务-使用ID刮取DIV标记内的内容,然后返回XHTML。 我正在使用“PHP简单HTML DOM解析器” 简化代码示例: <html><head></head> <body> <h1>Head</h1> <div class="page"> <div id="content"> <h2>Section head</h2> <p>Text</p> <

任务-使用ID刮取DIV标记内的内容,然后返回XHTML。 我正在使用“PHP简单HTML DOM解析器”

简化代码示例:

<html><head></head>
<body>
<h1>Head</h1>
<div class="page">
<div id="content">
<h2>Section head</h2>
<p>Text</p>
</div>
<div id="footer">Footer text</div>
</div>
</body>
</html>
$content现在是一个simpleDOM对象数组(已更正)

如何将其转换回XHTML,以便:

<div id="content">
<h2>Section head</h2>
<p>Text</p>
</div>

科长
正文

谢谢

您是否尝试过:

// Dumps the internal DOM tree back into string 
$str = $content->save();
参考:

这工作正常:

// Sample HTML string
$html_str = '<html><head></head><body><h1>Head</h1><div class="page"><div id="content"><h2>Section head</h2><p>Text</p></div><div id="footer">Footer text</div></div></body></html>';

// Create new DOM object
$dom = new DOMDocument();

// $html_str is HTML (can load from URL, if your host allows)
$dom->loadHTML($html_str);

// Get DIV id="content"
$element = $dom->getElementById('content');

// use save XML as input is XHTML.
echo $dom->saveXML($element);

// cleanup to prevent memory leak
$dom->clear(); 
unset($dom);
//示例HTML字符串
$html_str='HeadSection headText

Footer Text'; //创建新的DOM对象 $dom=新的DOMDocument(); //$html_str是html(如果您的主机允许,可以从URL加载) $dom->loadHTML($html\u str); //Get DIV id=“content” $element=$dom->getElementById('content'); //将XML另存为XHTML输入。 echo$dom->saveXML($element); //清理以防止内存泄漏 $dom->clear(); unset($dom);

如果在其他模板中使用,则必须添加正确的字符集才能正确显示字符

我已更正了我的问题$html->find(“#content”)返回的数组不是对象,因此不能使用$content->save();
// Sample HTML string
$html_str = '<html><head></head><body><h1>Head</h1><div class="page"><div id="content"><h2>Section head</h2><p>Text</p></div><div id="footer">Footer text</div></div></body></html>';

// Create new DOM object
$dom = new DOMDocument();

// $html_str is HTML (can load from URL, if your host allows)
$dom->loadHTML($html_str);

// Get DIV id="content"
$element = $dom->getElementById('content');

// use save XML as input is XHTML.
echo $dom->saveXML($element);

// cleanup to prevent memory leak
$dom->clear(); 
unset($dom);