Php 在元素中插入元素?
如何使用简单的HTMLDOM和普通的PHP向页脚添加链接?我使用此选项查找页脚:Php 在元素中插入元素?,php,simple-html-dom,Php,Simple Html Dom,如何使用简单的HTMLDOM和普通的PHP向页脚添加链接?我使用此选项查找页脚: $html = file_get_html('/index.php'); // Find footer foreach($html->find('footer') as $f){ //the normal php code here } 现在如何使用PHP将链接添加到页脚中 否则,如果您不需要简单的HTMLDOM就可以做到这一点,您该怎么做? 我宁愿使用普通的PHP而不是简单的HTMLDOM //
$html = file_get_html('/index.php');
// Find footer
foreach($html->find('footer') as $f){
//the normal php code here
}
现在如何使用PHP将链接添加到页脚中
否则,如果您不需要简单的HTMLDOM就可以做到这一点,您该怎么做?
我宁愿使用普通的PHP而不是简单的HTMLDOM
// create a dom
$dom = new DOMDocument();
// load the html url
$dom->loadHtmlFile('http://localhost/index.php');
// get the #footer element
$footer = $dom->getElementById('footer');
if ($footer) {
// create a link element
$link = $dom->createElement('a');
// set the href attribute
$link->setAttribute('href', '...');
// add some text content to the link
$link->appendChild($dom->createTextNode('...'));
// append the link into the footer
$footer->appendChild($link);
}
echo $dom->saveHtml();