SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can not parse comments inside script tags with XPATH?

    I expect the code below to produce something like
    "comment between script tags"
    "comment outside script tags"

    Instead it produces:
    NULL
    "comment outside script tags"

    Assuming my XPATH query is correct, why can't I parse the comment node inside the script tags?
    Is there something I am missing?

    Code:
    <?php
    $html = '
    <script>
    <!-- comment between script tags //-->
    </script>
    <!-- comment outside script tags //-->
    ';
    
    $dom = new DOMDocument();
    
    @$dom->loadHTML($html);
    
    $xpath = new DOMXPath($dom); //put $dom into xpath
    
    $xpath_query[] = '//script/comment()';
    $xpath_query[] = '//comment()';
    
    foreach($xpath_query as $xpath_query)
    	{
    		$match = $xpath->query($xpath_query)->item(0)->textContent; //query dom for match
    
    		echo '<pre>'; var_dump($match); echo '</pre><br />';
    	}
    
    ?>

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    echo $dom->saveXML();

    Should give you a clue as to why it isnt working...
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •