[英]PHP: extract multiple data from html table with xpath
我必须从HTML页面读取信息并将其传输到多个数组以进行进一步处理。 我使用xpath的方法还没有取得成功,以至于我无法访问所需的数据。
主体部分包含一个表,该表具有不同的行数,如以下示例所示:
...
</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>
...
我需要一个数组,其中包含第2行的title
属性作为键,并从<select>
部分(第12行)获取属性name
作为值。
$devices = [
'43PUS6551' => 'profile:user6418'
…
]
我从这里开始,我能够收到该数组的密钥:
$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']);
但是现在我正在努力获得指定的值。 我尝试了不同的方法:向上使用parent
,然后向下返回节点<select>
或使用following-sibling
。 但是我太傻了,不能正确使用xpath synthas。
如果做到这一点,我需要一个数组,其中包含<select>
部分(第12行)的属性name
作为键,以及<option>
部分的属性value
(也被selcted
为值)。
$filters = [
'profile:user6418' => 'filtprof5333'
…
]
最后,我需要一个数组,其中包含<option>
部分中的数据(出现在每一行中):
$profiles = [
'Standard' => 'filtprof1',
'Unbeschränkt' => 'filtprof3,
…
'X-Box-One' => 'filtprof4526',
]
适当的xpath提示的任何帮助将不胜感激
试试吧:
preg_match_all('/\<option value\="([a-z0-9]+)">([A-Za-z0-9\_\-]+)\<\/option\>/', $str, $match, PREG_SET_ORDER);
$profiles = array();
foreach($match as $row) {
$profiles[$row[2]] = $row['1'];
}
print_r($profiles);
以下功能对我来说很理想:
// convert html response into SimpleXML
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadHTML($response);
$xmlSite = simplexml_import_dom($dom);
// initialize processing values
$devices = [];
$options = [];
$filters = [];
// parse SimpleXML with xpath to get current data
$rows = $xmlSite->xpath('//tr/td[@title=@datalabel]'); // these are the rows with assignments of devices to filters
foreach ($rows as $row) {
$key = utf8_decode((string)$row->attributes()['title']); // name (label) of the devices
if (preg_match('/Alle /', $key)) { // skip standard settings
continue;
}
$select = $row->xpath('parent::*//select[@name]'); // find the line with the currently assigned ID for the device
$value = (string)$select[0]->attributes()['name']; // get the current ID ('profile:user*' or 'profile:landevice*')
$devices[$key] = $value;
$options = $select[0]->xpath('option'); // the defined filters (dropdown in each row)
foreach ($options as $option) {
$profiles[utf8_decode((string)$option)] = (string)$option->attributes()['value']; // get label and ID of filters
if (isset($option->attributes()['selected'])) { // determine the filter currently assigned to the device
$filters[$value] = (string)$option->attributes()['value']; // get device (ID) and filter (ID)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.