<tr class=" odd">
<td class="pos">DATA 1</td>
<td><a href="..." target="_top">DATA 2</a></td>
<td>DATA 3</td>
<td><a href="...">DATA 4</a></td>
<td>DATA 5</td>
<td>DATA 6</td>
<td>DATA 7</td>
<td><a href="...">DATA 8</a></td>
<td>DATA 9</td>
<td>DATA 10</td>
<td style="min-width:48px"><a href="...">
<img alt="..." src="..." style="padding-right: 4px; height: 20px; width: 20px">
</a></td>
</tr>
I am looking at the code above. This code shows one row of data, I am looking at around 7500 rows of data in this format. I would like to select specific columns of data, I mainly need DATA 2, DATA 3, DATA 6 and DATA 7.
I've made code that grabs DATA 2 and DATA 3 and stores them in an array which works ok. Code below:
$array_data2 = array();
$array_data3 = array();
$array_data6 = array();
while(FALSE != $data2_pos = strpos($output, "_top",$pos + 1)) {
$pos2 = substr($output, $data2_pos+6);
$pos3 = strpos($pos2, "</a>");
$data2= substr($pos2, 0, $pos3);
$data2=ltrim ($data2);
$data2=rtrim ($data2);
$array_data2[ ] = $data2;
$data3_pos=strpos($output,"<td>", $pos);
$pos22 = substr($output, $data3_pos+4);
$pos33 = strpos($pos22, "</td>");
$data3= substr($pos22, 0, $pos33);
$data3=ltrim ($data3);
$data3=rtrim ($data3);
$array_data3[ ] = $data3;
}
I am looking for a solution to select DATA 6. The above code works for DATA 2 because it looks for target="_top"
and for DATA 3 it looks for the first table <td>
tag. There does not seem to be any code that distinguishes DATA 6 from the other columns of data.
One approach that I have tried is using a fixed number of characters as below:
$data6_pos=strpos($output,"<td>",$pos);
$pos222 = substr($output, $data6_pos+100);
$pos333 = strpos($pos222, "</td>");
$data6= substr($pos222, 0, $pos333);
$data6=ltrim ($data6);
$data6=rtrim ($data6);
$array_data6[ ] = $data6;
The disadvantage with this is that DATA 6 is not always 100 characters after the first <td>
tag position.
I would be very grateful for any ideas with looking at separating sections of this data. Many thanks!
Based on the comments I have started using DOM as code below:
$dom = new DOMDocument;
$dom->loadHTML($output);
foreach ($dom->getElementsByTagName('tr') as $node) {
echo $node->nodeValue;
echo '<br>';
}
This is much shorter code and displays the values with a new line each time there is a new <tr>
table tag:
DATA 1 DATA 2 DATA 3 DATA 4 DATA 5 DATA 6 DATA 7 DATA 8 DATA 9 DATA 10
I'm now going to look into how I get separate sections of this data to store individual columns such as DATA 2 and DATA 3 into separate database fields.