0

オンライン時刻表 (学校用) から配列にレッスンを取得する必要があります。したがって、データをデータベースに挿入できます。オンライン時刻表 (url: roosters-hd.stenden.com ) は次のようになります。

左側には時刻が表示され、上部には学校の曜日 (月、火、日、木、金) が表示されます。非常に基本的です。

各レッスンには、取得する必要がある 6 つの値が含まれています。

それに加えて、[startDate] と [endDate] も取得する必要があります。時間は、レッスンセルがどの行にあるか、およびその行スパンに基づいています。日付は、開始日 (上に印刷) に列番号を追加することで計算できます。したがって、最終的に配列は次のようになります。

[0] => Array
        (
            [0] => Array
                (
                    [Name] => Financiering
                    [Type] => WC
                    [Code] => DECBE3
                    [Classroom] => E2.053 - leslokaal
                    [Teacher] => Verboeket, Erik (E)
                    [Class] => BE1F, BE1B, BE1A
                    [StartDate] => 04/06/2013 08:30:00
                    [EndDate] => 04/06/2013 10:00:00
                )
                etc.

私のデータ取得の経験が不足しているため、非常に非効率的で柔軟性のないソリューションになってしまいます。XMLパーサーを使用する必要がありますか?それとも正規表現?この問題に取り組む方法についてのアイデアはありますか?

4

1 に答える 1

2

正規表現の方法:

<pre><?php
$html = file_get_contents('the_url.html');

$clean_pattern = <<<'LOD'
~
  # definitions
    (?(DEFINE)
        (?<start>         <!--\hSTART\hOBJECT-CELL\h-->                    ) 
        (?<end>           (?>[^<]++|<(?!!--))*<!--\hEND\hOBJECT-CELL\h-->  )

        (?<next_cell>     (?>[^<]++|<(?!td\b))*<td[^>]*+>  ) 
        (?<cell_content>  [^<]*+                           )
    )

  # pattern
    \g<start>
        \g<next_cell>     (?<Name>      \g<cell_content>   )  
        \g<next_cell>     (?<Type>      \g<cell_content>   )
        \g<next_cell>     (?<Code>      \g<cell_content>   )

        \g<next_cell>     (?<Classroom> \g<cell_content>   )
        \g<next_cell>

        \g<next_cell>     (?<Teacher>   \g<cell_content>   )
        \g<next_cell>     
        \g<next_cell>     (?<Class>     \g<cell_content>   )
    \g<end>
~x
LOD;

preg_match_all($clean_pattern, $html, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
    echo <<<LOD
    Name: {$match['Name']}
    Type: {$match['Type']}
    Code: {$match['Code']}
    Classroom: {$match['Classroom']}
    Teacher: {$match['Teacher']}
    Class: {$match['Class']}<br/><br/>
LOD;
}

DOM/XPath の方法:

$doc = new DOMDocument();
@$doc->loadHTMLFile('the_url.html');
$xpath = new DOMXPath($doc);
$elements = $xpath->query("//*[comment() = ' START OBJECT-CELL ']");
$fields = array('Name', 'Type', 'Code', 'Classroom', 'Teacher', 'Class');
$not_needed = array(10,8,6,1,0);    
foreach ($elements as $element) {
    $temp = explode("\n", $element->nodeValue);
    foreach ($not_needed as $val) { unset($temp[$val]); }
    array_walk($temp, function (&$item){ $item = trim($item); });
    $result[] = array_combine($fields, $temp);
}   
print_r ($result);
于 2013-07-11T20:17:52.483 に答える