开发者

Can't find <p> in div id=footer

开发者 https://www.devze.com 2023-02-06 14:41 出处:网络
A questionrelated on my previous post,about Simple HTML DOM Parser. This is my html code: <div id=\"header\">

A question related on my previous post, about Simple HTML DOM Parser. This is my html code:

<div id="header">
<h1>Hello World!</h1>
</div>
<div id="container">
 <div id="content">
    <p> </p>
    </div>
</div>

<div id="footer">
<p>LINK HERE</p>
</div>

I want to insert a link on the <p> of the footer.I could insert a link but the problem is, if there is another <p> before the div footer's <p>, the link will be inserted at the <p> in the html code. It's like it can't identify or it cannot be detected that it SHOULD ONLY SEARCH IN THE div id=footer, not the whole html code.

Here's what i've got:

foreach($html->find('div#footer') as $footer) 
   {
    foreach($footer->find('p') as $p) 
   {
  $footerInnerText = $p->find('p', 0)->innertext;
  $theLink = '<a href="index.php开发者_如何学运维" title="" rel="home">'. $footerInnerText. '</a>';
  $html->find('p', 0)->innertext = "\n" .$theLink."\n";
  }

I have also tried this:

$e = $html->find('div#footer', 0)->find('p', 0);

What am i missing here?


I find Nodes in HTML a bit funky. I've seen a fair few problems with blank nodes, etc. For example in HTML the blank " " would parse as nothing, as you do not have any real text in it.

&nbsp; is the code you need for a space.

I believe the problem may be caused by the innerHTML of the P being blanked, therefore not existing. Have you tried by putting any values in it at all, just a random test line or the like?

0

精彩评论

暂无评论...
验证码 换一张
取 消