Help me parse the html
Please help me parse the html. I need to enter data in an array according to this principle:
$mas[0]=array(00094318,4 500,id5869445320,2014-08-03 14:48);
$mas[1]=array(00094366,5 500,id58694453765,2014-08-03 14:48);
I tried to parse:
preg_match_all('/\<td class="td[0-9]( text_right){0,1}"\>(.){0,}\<\/td\>/U',$html,$data);
But my version does not work quite correctly.
<table class="table2" cellpadding="0" cellspacing="0">
<tr class="tr_head">
<td class="td_head">Отправитель</td>
<td class="td_head" style="width:70px">Сумма, руб.</td>
<td class="td_head" style="width:310px">Комментарий</td>
<td class="td2">Дата проведения</td>
</tr>
<tr class="tr1" onmouseover="onMouseOver(this, '')" onmouseout="onMouseOut(this, '', 'tr1')">
<td class="td1">00094318</td><td class="td1 text_right">4 500</td>
<td class="td1">id5869445320</td>
<td class="td2">2014-08-03 14:48</td>
</tr>
<tr class="tr1" onmouseover="onMouseOver(this, '')" onmouseout="onMouseOut(this, '', 'tr1')">
<td class="td1">00094366</td><td class="td1 text_right">5 500</td>
<td class="td1">id58694453765</td>
<td class="td2">2014-08-03 14:48</td>
</tr>
<tr class="tr6">
<td class="padding_5_0 td2">Количество операций: <b>1</b></td>
<td class="td2 text_right bold">4 500</td>
<td></td>
<td></td>
</tr>
</table>
1
Author: Дмитрий Малышев, 2014-08-03
1 answers
Parsing HTML with the help of regulars, in my opinion and in the opinion of many people, is a perversion. For such things, there are appropriate tools: PHP Simple HTML DOM Parser or phpQuery (examples can be found here), or even using the built-in PHP tools. Here is your task, implemented using DOMDocument and without connecting any other third-party libraries:
$doc = new DOMDocument();
$doc->loadHTMLFile("file.html"); // путь к вашему файлу
$table = $doc->getElementsByTagName('table');
$data = array();
$i = 0;
foreach($table->item(0)->getElementsByTagName('tr') as $tr){
foreach($tr->getElementsByTagName('td') as $td){
$data[$i][] = htmlentities(utf8_decode($td->nodeValue), ENT_QUOTES, 'UTF-8');
}
$i++;
}
print_r($data);
Result:
Array
(
[0] => Array
(
[0] => Отправитель
[1] => Сумма, руб.
[2] => Комментарий
[3] => Дата проведения
)
[1] => Array
(
[0] => 00094318
[1] => 4 500
[2] => id5869445320
[3] =>
)
[2] => Array
(
[0] => 00094366
[1] => 5 500
[2] => id58694453765
[3] =>
)
[3] => Array
(
[0] => Количество операций: 1
[1] => 4 500
[2] =>
[3] =>
)
)
6
Author: Deonis, 2014-08-03 14:57:00