A
A
Andrey Petrov2014-08-21 15:53:06
PHP
Andrey Petrov, 2014-08-21 15:53:06

How to parse a certain cell of an HTML table and several subsequent ones?

Hello.
Stuck on the following issue. I have an HTML table:

<table>
 <tr>
   <td class="past">текст</td>
   <td class="past">текст</td>
   <td class="past">текст</td>
   ..........................
   <td class="today">текст</td>
   <td>текст</td>
   <td>текст</td>
 </tr>
 <tr>
   <td>текст</td>
   <td class="weekend">текст</td>
   <td class="weekend">текст</td>
   <td>текст</td>
   <td>текст</td>
   <td>текст</td>
 </tr>
</table>

The task is to use regular expressions to parse the entire contents of the cell and, in addition, the contents of 4 more cells immediately following it. Moreover, regardless of whether these cells have a class or not, and whether they are located in the current line or in the next. If there should be no problems with writing a regular expression specifically for a cell , then I can’t think of how to include these neighboring cells in the parsing. I will be glad for any help. Thank you. <td class="today"><tr>
<td class="today">

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Petrov, 2014-08-22
@anndmill

On the advice of 'AlexP11223' and after googling, I posted this code.
I feel that it is far from ideal, but everything works.
Comments are accepted.)

libxml_use_internal_errors(true);
$dom = new DomDocument;
$dom->loadHTMLFile("http://calendar.zoznam.sk/sunset-pl.php?city=3080866");
$xpath = new DomXPath($dom);

$today = $xpath->query("//td[@class='today']");
$nodes1 = $xpath->query("//td[@class='today']/following::td[1]");
$nodes2 = $xpath->query("//td[@class='today']/following::td[2]");
$nodes3 = $xpath->query("//td[@class='today']/following::td[3]");
$nodes4 = $xpath->query("//td[@class='today']/following::td[4]");

header("Content-type: text/plain");

foreach ($today as $i => $node) {
    echo $node->nodeValue, "\n";
}
foreach ($nodes1 as $i => $node1) {
    echo $node1->nodeValue, "\n";
}
foreach ($nodes2 as $i => $node2) {
    echo $node2->nodeValue, "\n";
}
foreach ($nodes3 as $i => $node3) {
    echo $node3->nodeValue, "\n";
}
foreach ($nodes4 as $i => $node4) {
    echo $node4->nodeValue, "\n";
}

+ If with evaluate, then it will be something like this:
$today = $xpath->evaluate('string(//td[@class="today"])');
$first = $xpath->evaluate('string(//td[@class="today"]/following::td[1])');
$second = $xpath->evaluate('string(//td[@class="today"]/following::td[2])');
$third = $xpath->evaluate('string(//td[@class="today"]/following::td[3])');
$fourth = $xpath->evaluate('string(//td[@class="today"]/following::td[4])');
  
echo 	$today."<br>",
    $first."<br>",
    $second."<br>",
    $third."<br>",
    $fourth;

A
AlexP11223, 2014-08-21
@AlexP11223

Why parse reg. HTML expressions?
stackoverflow.com/a/1732454/964478
There is, for example, XPath.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question