B
B
bigrusssianshark2019-10-28 01:58:08
PHP
bigrusssianshark, 2019-10-28 01:58:08

How to parse data with a specific value?

Hello!
There is a web page on which there is a regularly changed table. In the code of this table, this code is repeated N-th number of times

<tr>
      <td class="highlightable group">ГРУППА</td>
      <td class="pnum highlightable">НОМЕР</td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ</p><p class="pteacher">ИМЯ<span class="pcab"></span></p></td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ 2</p><p class="pteacher">ИМЯ 2<span class="pcab">НОМЕР 2</span></p></td>
    </tr>

moreover, with different meanings in place of the word "group", etc. The
question is: how to use PHP without third-party libraries to parse the above code from the table with a certain value in place of the word "group" N-th number of times (that is, to filter out all the code where in place of "group" is not a suitable value)
For example, the code on the page was
<tr>
      <td class="highlightable group">НЕ ПОДХОДИТ</td>
      <td class="pnum highlightable">НОМЕР</td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ</p><p class="pteacher">ИМЯ<span class="pcab"></span></p></td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ 2</p><p class="pteacher">ИМЯ 2<span class="pcab">НОМЕР 2</span></p></td>
    </tr>
    <tr>
      <td class="highlightable group">НЕ ПОДХОДИТ</td>
      <td class="pnum highlightable">НОМЕР</td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ</p><p class="pteacher">ИМЯ<span class="pcab"></span></p></td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ 2</p><p class="pteacher">ИМЯ 2<span class="pcab">НОМЕР 2</span></p></td>
    </tr>
    <tr>
      <td class="highlightable group">ПОДХОДИТ</td>
      <td class="pnum highlightable">НОМЕР</td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ</p><p class="pteacher">ИМЯ<span class="pcab"></span></p></td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ 2</p><p class="pteacher">ИМЯ 2<span class="pcab">НОМЕР 2</span></p></td>
    </tr>
    <tr>
      <td class="highlightable group">НЕ ПОДХОДИТ</td>
      <td class="pnum highlightable">НОМЕР</td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ</p><p class="pteacher">ИМЯ<span class="pcab"></span></p></td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ 2</p><p class="pteacher">ИМЯ 2<span class="pcab">НОМЕР 2</span></p></td>
    </tr>
    <tr>
      <td class="highlightable group">ПОДХОДИТ</td>
      <td class="pnum highlightable">НОМЕР</td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ</p><p class="pteacher">ИМЯ<span class="pcab"></span></p></td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ 2</p><p class="pteacher">ИМЯ 2<span class="pcab">НОМЕР 2</span></p></td>
    </tr>
    <tr>
      <td class="highlightable group">НЕ ПОДХОДИТ</td>
      <td class="pnum highlightable">НОМЕР</td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ</p><p class="pteacher">ИМЯ<span class="pcab"></span></p></td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ 2</p><p class="pteacher">ИМЯ 2<span class="pcab">НОМЕР 2</span></p></td>
    </tr>

And the output was
<tr>
      <td class="highlightable group">ПОДХОДИТ</td>
      <td class="pnum highlightable">НОМЕР</td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ</p><p class="pteacher">ИМЯ<span class="pcab"></span></p></td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ 2</p><p class="pteacher">ИМЯ 2<span class="pcab">НОМЕР 2</span></p></td>
    </tr>
    <tr>
      <td class="highlightable group">ПОДХОДИТ</td>
      <td class="pnum highlightable">НОМЕР</td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ</p><p class="pteacher">ИМЯ<span class="pcab"></span></p></td>
      <td class="highlightable onepair"><p class="pname">ПРЕДМЕТ 2</p><p class="pteacher">ИМЯ 2<span class="pcab">НОМЕР 2</span></p></td>
    </tr>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2019-10-28
@bigrusssianshark

$dom = new DOMDocument;
$dom->loadXML($html);
 
$content = '';
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//td[.="ПОДХОДИТ"]/..') as $row) {
  $content .= $dom->saveXML($row) . PHP_EOL;
}
echo $content;
https://ideone.com/p3zvcJ

D
Developer, 2019-10-28
@samodum

Regulars are easy to parse
https://regex101.com/r/3PSJ0u/1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question