Potion Potion - 8 days ago 6
HTML Question

PHP DOM Parser Find First Child of Div With Specific Class and Search Each Elements

As the title says Im trying to find the first child of an element with a specific class here is what I would like to do:

Update: I added the format. Also I figured how to get to the tbody. But at the end of the day what I am trying to do is check each element within tbody if they each equal to "--" or not. How would I achieve this then? Would I convert the DOM Object to a string? Whats an efficient way to do this.

Update 2: I found an elegant solution which I updated to my code. Still the question lingers: Whats the best way to check if there are integers? Do I just convert each element to a string and then do a strpos condition?

Update 3: Nevermind I figured it out.Included below is the solution. I had to go one step further into the table body to select the table rows and loop through them instead.



foreach($html->find('div[class=compareSkillsOuter clear] section[class=playerStats] div[class=tiledBlueFrame] div[class=tableWrap] table[class=headerBgLeft] tbody tr') as $element){
if($position = strrpos($element, '--');
if($position !== false){
echo "String found";
}
else{
echo "String not found";
}
}





Format:



<div class="compareSkillsOuter clear">
<section class="playerStats">
<div class="tiledBlueFrame">
<div class="tl corner"></div>
<div class="tr corner"></div>
<div class="bl corner"></div>
<div class="br corner"></div>
<div class="tableWrap">
<table class="headerBgLeft">
<thead>
<tr>
<th class="alignleft">Rank</th>
<th class="alignleft">Total XP</th>
<th class="alignleft">Level</th>
</tr>
</thead>
<tbody>
<tr >
<td class="alignleft"><a href="#">1,700,012</a></td>
<td class="alignleft"><a href="#">3,290</a></td>
<td class="playerWinLeft alignleft"><div class="relative"><a href="#">55</a></div></td>
</tr>
<tr class=oddRow>
<td class="alignleft"><a href="#">--</a></td>
<td class="alignleft"><a href="#">--</a></td>
<td class="alignleft"><a href="#">--</a></td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</div>





Best Solution:



$data = $html->find("table[class=headerBgLeft] tbody",0);
$data1 = $data->find("tr td a");

$statistics = Array();


//echo $data1[1]->plaintext;

foreach($data1 as $tr)
{
//store each
echo "$tr 1<br>";
}




Answer

If You want to find tr body than just use table and tbody element. find always return an array and your if statement is with semicolon.

$data= $html->find("table[class=headerBgLeft] tbody");
$data1=$data->find("td tr");
foreach($data1 as $ed)
 {
    if($ed->innertext=="--")
      echo "String is found";
    else
      echo "String is not found";
 }

If you only need to find that two dashes within tbody and their position is fixed means they always in second tr. You can use

  $data1=$data->find("td tr",1);

If it is not your problem. Or it does not work please comment. I have not tested it.

Comments