I
I
IG512017-08-16 15:17:32
PHP
IG51, 2017-08-16 15:17:32

How to fix a broken parser?

Hello, there was a working parser (I didn’t see it in working condition) that took data from a table, from a page and uploaded it to a page on another site, but it stopped working, the layout on the original site may have changed or something else, I will be glad for any help , Thank you.

<?
function get_news_list($link) 
  {
    // Загружаем HTML-страницу 
    $fd = fopen($link, "r");
    $text=""; 
    if (!$fd) echo "Ошибка загрузки расписания: ".$link; 
    else 
    { 
      // Чтение содержимого файла в переменную $text 
      while (!feof ($fd)) $text .= fgets($fd, 4096); 
    }

// Закрыть открытый файловый дескриптор 
    fclose ($fd);
    return $text;
} // Конец функции

?>

<?
if(CModule::IncludeModule("iblock")) {
  $content=get_news_list('https://1c.ru/cso-part/rus/partners/training/cso/timetable?country=00'); // Получаю расписание
  //$text=iconv("CP1251","UTF-8",$content);
  $pattern='|<table width="100%" border="1" cellspacing="0" cellpadding="5" class="content" style="margin-top: 80px; margin-bottom: 30px;">.*?</table>|s';
  preg_match($pattern, $content, $table);
//die($table[0]);
  $pattern='/(\d{2}-\d{2}\s\w{3,}\s|\d{2}\s\w{3,}\s-\s\d{2}\s\w{3,}\s)/';
  preg_match_all($pattern, $table[0], $dates);
  $pattern='/\d{2}:\d{2}-\d{2}:\d{2}/';
  preg_match_all($pattern, $table[0], $times);
  $pattern='/course[^\w]id=(\d{1,5})/';
  preg_match_all($pattern, $table[0], $ids);
  $all_cells=Array();
  $cycle=0;
  foreach($dates[0] as $key => $value) {
    //die($cycle);
    $all_cells[$cycle][0]=trim($value);
    $all_cells[$cycle][1]=$times[0][$cycle];
    $all_cells[$cycle][2]=$ids[1][$cycle];
    $arFilter=Array("IBLOCK_ID"=>"","PROPERTY_code1s"=>$ids[1][$cycle]);
    $arSelect = Array("ID", "NAME", "DETAIL_PAGE_URL","PREVIEW_TEXT");
    $res = CIBlockElement::GetList(Array(), $arFilter, false, Array(), $arSelect);
      while($ob = $res->GetNextElement())
        {
          $arFields = $ob->GetFields();
          $all_cells[$cycle][3]=$arFields["NAME"];
          $all_cells[$cycle][4]=$arFields["DETAIL_PAGE_URL"];
          $all_cells[$cycle][5]=$arFields["ID"];
          $all_cells[$cycle][6]=$arFields["PREVIEW_TEXT"];          
        }
    $cycle++;
  }
}
?>

<table class="table" style="border-collapse: collapse; ">
  <tbody>
    <tr align="center">
      <th class="left">Название курса</th>
      <th class="middle"><center>Дата проведения</center></th>
      <th class="right"><center>Оформить заявку</center></th>
    </tr>
  <?
  $color='#f1f1f1';
    foreach($all_cells as $key=>$value):?>
      <? //echo "111"; ?>

      <tr style="background-color:<?=$color?>">
        <td valign="middle"><a href="detail.php?COURSE_ID=<?=$all_cells[$key][5]?>" class="modern"><?=$all_cells[$key][3]?></a>
          <span id="hide<?=$all_cells[$key][5]?>" class="config-visible">
          <a href="javascript:ShowFull('<?=$all_cells[$key][5]?>')" title="Открыть полное описание"><img src="<?=SITE_TEMPLATE_PATH?>/images/open.png" border="0" class="open"></a>
          </span>
          <span id="show<?=$all_cells[$key][5]?>" class="config-hidden" style="display:none;">
          <div style="padding:10px;">
          <?=$all_cells[$key][6]?>
          <a href="javascript:HideFull('<?=$all_cells[$key][5]?>')" title="Свернуть описание"><img src="<?=SITE_TEMPLATE_PATH?>/images/hide.png" border="0" class="hide"></a>
          </div></span>
        </td>

        <td valign="middle" width=20%><center><b><?=$all_cells[$key][0]?><br><?=$all_cells[$key][1]?></b></center></td>
        
        <td valign="middle"><center><a style="margin-top:0px;text-align: center;padding: 9px 5px 9px 5px;" href="registration.php?COURSE_ID=<?=$all_cells[$key][5]?>" class="btn 1">Регистрация</a></center></td>
      </tr>

    <?if($color=="#f1f1f1"): $color="#fff"; else: $color="#f1f1f1"; endif;?>
    <?endforeach;?>
  </tbody>
</table>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Exploding, 2017-08-16
@IG51

It is unlikely that anyone will understand completely, but it is immediately clear that the pattern in the $pattern variable no longer matches html.
In that case, I would rewrite it for myself.
And parse html using the php method as in the example - it's all the same that there is soup with a fork)) You can, of course, rewind the fork with tape, etc., but why not use jq, for example, the same.

S
Stanislav B, 2017-08-16
@S_Borchev

Typical bitrix.
Probably the layout has changed. $dates will be empty.
there on the page there is no space after the date, and there are dates without a range.
here, try this pattern for the date
, and of course it’s horror, think about rewriting it using library norms

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question