-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeiseplanParser.php
45 lines (36 loc) · 1.62 KB
/
SpeiseplanParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
class SpeiseplanParser{
function __construct($url){
$this->dom = new DomDocument;
$this->dom->loadHTMLFile($url);
$this->xpath = new DomXPath($this->dom);
$this->descrNodes = $this->xpath->query("//div[contains(@class,'speiseplan-kurzbeschreibung')]");
$this->priceNodes = $this->xpath->query("//div[contains(@class,'speiseplan-preis')]");
$this->dateNodes = $this->xpath->query("//td[contains(@class,'speiseplan-head')]");
$this->speiseplan = array();
$this->days = array("monday", "tuesday", "wednesday", "thursday", "friday");
}
function parse(){
if(null !== $this->descrNodes->item(0)){
$domElementIndex = 0;
$domDateIndex = 0;
foreach($this->days as $day){
$this->speiseplan[$day] = array();
$this->speiseplan[$day]["date"] = substr(trim($this->dateNodes->item($domDateIndex)->nodeValue),-6);
$this->getDay($domElementIndex,$day,"meal1");
$domElementIndex++;
$this->getDay($domElementIndex,$day,"meal2");
$domElementIndex++;
$domDateIndex++;
}
return $this->speiseplan;
}
}
function getDay($index,$day,$meal){
if (null !== $this->descrNodes->item($index)){
$this->speiseplan[$day][$meal] = array( "description" => trim($this->descrNodes->item($index)->nodeValue), "price" => trim($this->priceNodes->item($index)->nodeValue));
}else{
$this->speiseplan[$day][$meal] = array( "description" => null, "price" => null);
}
}
}