0

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?

Alohci
  • 78,296
  • 16
  • 112
  • 156
woninana
  • 3,409
  • 9
  • 42
  • 66
  • It seems like there are parts missing from your question. I can't understand what you're asking for. Where do you want to insert a link? "I want to insert a link on the of the footer", What does that mean? – Olhovsky Jan 26 '11 at 05:54
  • How about using regex and preg_replace. Your Regex being something similar to : "/(div id=\"footer\">).*(

    ).*(

    ).*()/"
    – Flying Swissman Jan 26 '11 at 06:56
  • 2
    @Flying Swissman: [NO](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – BoltClock Jan 26 '11 at 07:05
  • i can't use regex to parse html – woninana Jan 26 '11 at 08:03
  • @BoltClock Yes it shouldn't be used for parsing a random piece of html but seeing the example I thought it was of very simple one off and predetermined piece of HTML. My response was to hasty. – Flying Swissman Jan 26 '11 at 08:17
  • I've not used the parser, but from the looks of the documentation, it supports descendant selectors, so you should just be able to do `$html->find('#footer p',0)->innertext = $theLink;`, or something similar. Also, why are you trying to insert `\n` s? Surely you'd want something like a `
    ` for HTML?
    – Matt Gibson Jan 26 '11 at 10:38

1 Answers1

0

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?

corrodedmonkee
  • 373
  • 1
  • 5