V
V
Vladimir2015-12-11 15:54:51
PHP
Vladimir, 2015-12-11 15:54:51

parsing html. Mistake. How to write correctly?

Good day.
Essence: The code worked on php 5.4. Moved to php 5.5 (another server). DiDom
library I got a syntax error in the code, before that everything worked fine. Well, I think it's because of the versions. But I don’t know how to write correctly, I tried and tried, it doesn’t work. :c

<?php
use DiDom\Document;
class Task_Matches extends Minion_Task {
  
    protected static $_timeout = 3;
  
    protected function _execute(array $params) {
        $matches = array();
        // Загрузка HTML кода страницы матчей
    $document = new Document('http://www.hltv.org/results/', true);
  
    $elements = $document->find('div.matchListRow');
        
        // Преобразование матчей до массива
        foreach($elements as $element) {	
      $match['match_format'] = $element->find('div.matchTimeCell')[0]->text(); // ТУТ ОШИБКА 
            //PHP Parse error:  syntax error, unexpected '['
            $match['team1_name'] = $element->find('div.matchTeam1Cell')[0]->text();
            $match['team1_logo'] = $element->find('div.matchTeam1Cell')[0]->find('span')[0]->find('img')[0]->attr('src');
            $match['result1'] = $element->find('div.matchScoreCell')[0]->find('span')[0]->text();
            $match['result2'] = $element->find('div.matchScoreCell')[0]->find('span')[1]->text();
            $match['team2_name'] = $element->find('div.matchTeam2Cell')[0]->find('a')[0]->text();
      $match['team2_logo'] = $element->find('div.matchTeam2Cell')[0]->find('span')[0]->find('img')[0]->attr('src');
      $match['details_link'] = $element->find('div.matchActionCell')[0]->find('a')[0]->attr('href');
      $match['enabled'] = 1;
      $match['date'] = time();
            
            $matches[] = $match;
    Minion_CLI::write($match['date']);
        }
        Minion_CLI::write('Detected '. count($matches) . ' matches ');

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2015-12-12
@Warderino

I solved the problem by doing this:
ps I don't like it very much...

<?php
use DiDom\Document;
class Task_Matches extends Minion_Task {

    protected static $_timeout = 3;
    /**
     * @param array $params
     */
    protected function _execute(array $params) {
        $matches = array();
        // Загрузка HTML кода страницы матчей
        $document = new Document('http://www.hltv.org/results/', true);
        $elements = $document->find('div.matchListRow');

        // Преобразование матчей до массива
        foreach($elements as $element) {
            //Временный багфикс
            $match_format = $element->find('div.matchTimeCell');
            $team1_name = $element->find('div.matchTeam1Cell');
            $team1_logo = $element->find('div.matchTeam1Cell span img');
            $result1 = $element->find('div.matchScoreCell span');
            $result2 = $element->find('div.matchScoreCell span');
            $team2_name = $element->find('div.matchTeam2Cell');
            $team2_logo = $element->find('div.matchTeam2Cell span img');
            $details = $element->find('div.matchActionCell a');

            //Получаем данные 1-го матча
            $match = array(
                'match_format' 	=> $match_format[0]->text(),
                'team1_name' 	=> $team1_name[0]->text(),
                'team1_logo'	=> $team1_logo[0]->attr('src'),
                'result1' 		=> $result1[0]->text(),
                'result2'		=> $result2[1]->text(),
                'team2_name' 	=> $team2_name[0]->text(),
                'team2_logo'	=> $team2_logo[0]->attr('src'),
                'details_link'	=> $details[0]->attr('href'),
                'enabled'	 	=> 1,
                'date' 			=> time(),
            );
            $matches[] = $match;
            Minion_CLI::write($match['date']);
        }

        Minion_CLI::write('Detected: '. count($elements) . ' matches');

         foreach($matches as $match) {
             $match_orm = new Model_Match;

             // Загрузка матча, если он уже есть в БД
             $match_orm->where('details_link', '=', $match['details_link'])->find();


             // Внесение новых данных о матче
             $match_orm->values($match);


             $match_orm->save();
         }

        Minion_CLI::write('Ready!');
    }
}

N
Nicholas, 2015-12-11
@healqq

$match = [
'match_format' => $element->find('div.matchTimeCell')[0]->text()
];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question