Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.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
Php 在元素中插入元素?_Php_Simple Html Dom - Fatal编程技术网

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 //

如何使用简单的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

// 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();