我正在尝试用simpledom html:(https://simplehtmldom.sourceforge.io/manual.htm)中的内容(摘要)替换html页面中的'richtext‘元素
一个数组变量‘sentence1’,带有“语句”=>摘要和sentence2等
我试过了
foreach ($summary->sentences as $sentence) {
$outhtml->find('div[class=richtext]',index)->outertext='<p>'.$sentence.'</p>';
index++;
}这会将数组中的最后一个元素保存到html中
foreach ($summary->sentences as $sentence) {
$outhtml->find('div[class=richtext]',0)->outertext='<p>'.$sentence.'</p>';
}预期结果
<div class='richtext'>
<p>sentence 1</p>
<p>sentence 2</p>
<p>sentence 3</p>
</div>发布于 2019-06-27 17:41:42
您可以使用implode()来构建内容,然后从该内容设置内部文本,而不是使用循环(这很容易覆盖以前的文本)。
$html->find('div[class=richtext]',0)->innertext = "<p>".
implode("</p><p>", $summary->sentences).
"</p>";https://stackoverflow.com/questions/56787783
复制相似问题