I have to read information from an HTML page and transfer it to multiple arrays for further processing. My approaches with xpath have not been so successful that I had access to the data I wanted.
The body section contains a table with different numbers of lines, as in the following example:
...
</tr>
<tr>
<td class="name" title="43PUS6551" datalabel="43PUS6551">
<span>43PUS6551</span>
</td>
<td datalabel="Internetnutzung" class="usage">eingeschränkt</td>
<td datalabel="Onlinezeit heute" class="bar time">
<span title="03:20 von 14:00 Stunden">
<span style="width:23.81%;"/>
</span>
</td>
<td datalabel="Zugangsprofil" class="profile">
<select name="profile:user6418">
<option value="filtprof1">Standard</option>
<option value="filtprof3">Unbeschränkt</option>
<option value="filtprof4">Gesperrt</option>
<option value="filtprof5334">Network</option>
<option value="filtprof5333" selected="selected">Stream</option>
<option value="filtprof4526">X-Box_One</option>
</select>
</td>
<td datalabel="" class="btncolumn">
<button type="submit" name="edit" id="uiEdit:user6418" value="filtprof5333" class="icon edit" title="Bearbeiten"/>
</td>
</tr>
<tr>
...
I need one array, which contains the title
attribute from line 2 as key and gets the attribute name
from the <select>
section (line 12) as value.
$devices = [
'43PUS6551' => 'profile:user6418'
…
]
I started with this and I´m able to receive the keys for this array:
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadHTML($response);
$xmlSite = simplexml_import_dom($dom);
$devices = [];
$rows = $xmlSite->xpath('//tr/td[@title=@datalabel]');
foreach ($rows as $row) {
$key = utf8_decode((string)$row->attributes()['title']);
But now I'm struggling to get the designated value. I tried different ways: upwards with parent
and back down to the node <select>
or with following-sibling
. But I'm too stupid to use the xpath synthas properly.
If I accomplished that, I need an array which contains the attribute name
from the <select>
section (line 12) as key and the attribute value
from the <option>
section which is also selcted
as value.
$filters = [
'profile:user6418' => 'filtprof5333'
…
]
Finally, I need one array containing the data from the <option>
section (appears in every row):
$profiles = [
'Standard' => 'filtprof1',
'Unbeschränkt' => 'filtprof3,
…
'X-Box-One' => 'filtprof4526',
]
Any help for propper xpath-hints will be appreciated