简体   繁体   English

如何使用SimpleHtmlDOM在html文件中插入一行php代码

[英]How to insert line of php code in html file, using SimpleHtmlDOM

I'm trying to insert THE PHP CODE between the <div id="container"> THE PHP CODE HERE </div> . 我正在尝试在<div id="container">这里的PHP代码</div>之间插入THE PHP CODE I'm using the http://simplehtmldom.sourceforge.net/ in this Php Html Parser. 我在此Php HTML解析器中使用http://simplehtmldom.sourceforge.net/

I've got this so far, how to find a div having an id called container: 到目前为止,我已经掌握了如何找到一个具有ID的容器div的方法:

// Find all which attribute id=foo //查找所有哪个属性id = foo

$ret = $html->find('div[id=foo]');

Then after finding the foo, what's next? 然后在找到foo之后,下一步是什么?

$html->find('div[id=foo]')->innertext = 'bar';

应该可以完成工作,但是您最好用JS修改DOM?

Why not modify the source Wordpress template instead of trying to hack in the text later? 为什么不修改源Wordpress模板,而不是稍后尝试修改文本? That PHP snippet could potentially produce a huge chunk of text, and you can't insert it verbatim into the DOM - you can only insert its output (which you'd have to capture with ob_start()/ob_get_clean() anywyas). 该PHP代码段可能会产生大量文本,并且您不能将其原样插入DOM-您只能插​​入其输出(必须使用ob_start()/ob_get_clean() anywyas捕获该输出)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM