Jump to content

using DOM in php


Jocka

Recommended Posts

I am trying to write a simple (or at least i thought it was simple) code using DOM to catch data between tags. such as:

<table width="100%" cellpadding="2" cellspacing="2">
<tr>
	<td colspan="2" align="center">
		TEST
	</td>
</tr>

<div class="test_row">

<tr>
	<td width="50%">
		<//ROW_NUM//>
	</td>
</tr>

</div>

</table>

 

then everything goes wrong here. trying to figure out how to catch the data in <div class="test_row">

<?php

function get_between_tags($html, $tag){

$domdoc= new DOMDocument();
$domdoc->loadHTML($html);
$domx = new DOMXPath($domdoc);
$items = $domx->query("*/div[@class=\"".$tag."\"]");

return $items->item(0)->nodevalue;

}
$html = file_get_contents("test.html");
$row = get_between_tags($html, "test_row");

?>

This was actually a code I found online but it called for $domx->execute which it says EXECUTE isn't a function or something.. .. any help would be appreciated.

I tried GOOGLEING php and DOM since the php website isn't much help on it, but i'm having a hard time finding any tutorials or info at all really.

Link to comment
https://forums.phpfreaks.com/topic/216426-using-dom-in-php/
Share on other sites

working on the code.. can't seem to get it to work ..

<?php

function get_between_tags($html, $tag){

$domdoc= new DOMDocument();
$domdoc->validateOnParse = true;
$domdoc->loadHTML($html);
//$domx = new DOMXPath($domdoc);
//$items = $domx->query("*/div[@class=\"".$tag."\"]");

return $domdoc->getElementById($tag)->innerHtml;

}
?>

 

when I try to echo it, it says: "Notice: Trying to get property of non-object in C:\wamp\www\template.php on line 18"

which line 18 is "return $domdoc->getElementById($tag)->innerHtml;"

Link to comment
https://forums.phpfreaks.com/topic/216426-using-dom-in-php/#findComment-1124684
Share on other sites

somebody help.. lol

 

This works MOSTLY

<?php
function get_between_tags($html, $tag){

$domdoc= new DOMDocument;
//$domdoc->validateOnParse = true;
$domdoc->loadHTML($html);
$div_tags = $domdoc->getElementById($tag);

return $domdoc->savexml($div_tags);

}
?>

 

it ALMOST works. Except it includes the DIV tags:

<div class="testrow" id="testrow">

<tr><td width="50%">
		ROW_NUM
	</td>

</tr></div>

so when I edit it, it repeats the DIV which just adds all this extra DIV code that I don't want or need. Please help me out here.. this is ridiculous. It seems A LOT faster than the old regex code I used to use but I just can't figure out why i cant get JUST the innerHTML from the DIV tag.

Link to comment
https://forums.phpfreaks.com/topic/216426-using-dom-in-php/#findComment-1124697
Share on other sites

I don't like to use others files. Just makes me feel like I'm cheating lol.

 

BUT I did find a solution of anyone wants to copy it somewhere for future reference. This function I wrote works pretty well (although I must admit the main code I needed was found here http://refactormycode.com/codes/708-innerhtml-of-a-domelement )

 

<?php

function get_between_tags($html, $tag){	

$domdoc= new DOMDocument;
$domdoc->validateOnParse = true;
$domdoc->loadHTML($html);

// FINDING THE TAG BY ID
$dom_tags = $domdoc->getElementById($tag);

// GET INNERHTML
 $parse_doc = new DOMDocument();
 foreach ($dom_tags->childNodes as $child){
	$parse_doc->appendChild($parse_doc->importNode($child, true));
 }

$dom_array = '';
$dom_array[0] = html_entity_decode($domdoc->saveXML($dom_tags));
$dom_array[1] =  $parse_doc->saveHTML();
return $dom_array;
}

$html_info ="<html>
<body>
<div id=\"thisrow\">
    <div id=\"thatrow\">
          <div id=\"testrow\">
                THIS IS THE TEST ROW
           </div>
     </div>
</div>
</body>
</html>";

// $tag_array holds both html/text WITH tags [0] and without tags [1]
$tag_array = get_between_tags($html_info, 'testrow');
print_r ($tag_array);

?>

 

which prints:

Array
(
    [0] =>           <div id="testrow">
                THIS IS THE TEST ROW
           </div>
    [1] =>
          
                THIS IS THE TEST ROW
           
)

I hope this helps someone else it. It seems much faster than the old preg_match I used to do.

Link to comment
https://forums.phpfreaks.com/topic/216426-using-dom-in-php/#findComment-1126028
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.