-1
    require_once 'phpDomClass.php';
    $html = '<div> 
            <div class="man">Name: madac</div>
            <div class="man">Age: 18
            <div class="man">Class: 12</div>
</div>' 
    $name=$html->find('div[class="man"]', 0)->innertext;
    $age=$html->find('div[class="man"]', 1)->innertext; 
    $cls=$html->find('div[class="man"]', 2)->innertext;

wanna get a text from each div class="man" but it didn't work because there is a missing closing div tag on 2nd line of html code. please help me to fix this.

thanks in advance.

1 Answers1

0

In this case you have to do some ugly string manipulation before you try and parse it with the DOM parser. If this is a one off, you can do something like this

<?php
require_once 'phpDomClass.php';
$html = '<div> 
  <div class="man">Name: madac</div>
  <div class="man">Age: 18
  <div class="man">Class: 12</div>
</div>';
$html = str_replace('<div class="man">Class:','</div><div class="man">Class:',$html);

$dom = new DomDocument();
$dom->loadHTML($html);
$name=$html->find('div.man', 0)->innertext;
$age=$html->find('div.man', 1)->innertext; 
$cls=$html->find('div.man', 2)->innertext;
Kinglish
  • 23,358
  • 3
  • 22
  • 43