DOMDocument(); getElementById in PHP

Discussion in 'PHP' started by LimeBlast, Apr 10, 2009.

  1. #1
    Hi everyone...

    I am trying to getElementById (like in javascript) but this time in PHP. But, I have a problem :mad:...

    
    <?PHP
    //Add the content on the page
      $doc = new DOMDocument();
      $doc->validateOnParse = true;
      $doc->loadHTMLFile('/home/limebl5/public_html/ms/templates/' . $templateid . '/index.php');
      
      //echo innerHTML content of the area id (this is where I have a problem)
      echo $doc->getElementById('area');
    ?>
    
    PHP:
    the html document looks like this...

    <div id="area">yeah this is the content that I want to show...</div>
     
    LimeBlast, Apr 10, 2009 IP
  2. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    $html = file_get_contents('/home/limebl5/public_html/ms/templates/' . $templateid . 'index.php');
    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $div = $dom->getElementByID('area');
    echo $div->firstChild->data; 
    ?>
    PHP:
    Should work, I think.

    InnerHTML won't work because it's not actually part of the DOM, just a pseudo property added by browsers.
     
    JDevereux, Apr 11, 2009 IP
  3. LimeBlast

    LimeBlast Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for the reply, buy it still does not work.I think I will need to find an alternative...
     
    LimeBlast, Apr 13, 2009 IP
  4. antigravity

    antigravity Active Member

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #4
    Try this...

    <?PHP
    //Add the content on the page
      $innerHTML = '';
      $doc = new DOMDocument();
      $doc->validateOnParse = true;
      $doc->loadHTMLFile('/home/limebl5/public_html/ms/templates/' . $templateid . '/index.php');
     
      $children = $doc->childNodes;
      foreach ($children as $child) {
        $tmp_doc = new DOMDocument();
        $tmp_doc->appendChild($tmp_doc->importNode($child,true));       
        $innerHTML .= $tmp_doc->saveHTML();
      }  
    
    //echo innerHTML content of the area id (this is where I have a problem)
      echo $innerHTML;
    ?>
    PHP:
     
    antigravity, Apr 13, 2009 IP
  5. LimeBlast

    LimeBlast Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you antigravity for you're replay...

    I tried you're code but it returns...

    Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, boolean given in /home/limebl5/public_html/ms/view.php on line 132

    Line 132 is: $tmp_doc->appendChild($tmp_doc->importNode($child,true));

    My ultimate goal is to load a template, replace the ids with the users content and send it to the users computer.
     
    LimeBlast, Apr 13, 2009 IP