Skip to content

Instantly share code, notes, and snippets.

@szepeshazi
Last active March 31, 2017 11:30
Show Gist options
  • Save szepeshazi/76dbdc35d16f99789ed0217f70732138 to your computer and use it in GitHub Desktop.
Save szepeshazi/76dbdc35d16f99789ed0217f70732138 to your computer and use it in GitHub Desktop.
XML Parser for TourRadar tours
<?php
require 'toursxml.php';
function xmlToCSV($text) {
$columns = ['Title', 'Code', 'Duration', 'Inclusions', 'MinPrice'];
$separator = '|';
$header = implode($separator, $columns);
echo $header . PHP_EOL;
try {
$tours = new SimpleXMLElement($text);
} catch (Exception $e) {
error_log('Could not parse input: ' + $e->getMessage());
return;
}
foreach ($tours->TOUR as $tour) {
echo htmlspecialchars_decode($tour->Title) . $separator;
echo $tour->Code . $separator;
echo (string)$tour->Duration . $separator;
echo trim(mb_ereg_replace("\s+", " ", html_entity_decode(strip_tags($tour->Inclusions)))) . $separator;
$minPrice = null;
foreach ($tour->DEP as $departure) {
$price = floatval($departure['EUR']);
if (isset($departure['DISCOUNT'])) {
$discount = floatval(rtrim($departure['DISCOUNT'], '%'));
$price = $price * (100.0 - $discount) / 100.0;
}
if ($minPrice === null || $price < $minPrice) {
$minPrice = $price;
}
}
if ($minPrice !== null) {
echo number_format($minPrice, 2, '.', '');
}
echo PHP_EOL;
}
}
xmlToCSV($toursXML);
?>
<?php
$toursXML = <<<XML
<?xml version="1.0"?>
<TOURS>
<TOUR>
<Title><![CDATA[Anzac &amp; Egypt Combo Tour]]></Title>
<Code>AE-19</Code>
<Duration>18</Duration>
<Start>Istanbul</Start>
<End>Cairo</End>
<Inclusions>
<![CDATA[<div style="margin: 1px 0px; padding: 1px 0px; border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; text-align: justify; line-height: 19px; color: rgb(6, 119, 179);">The tour price&nbsp; cover the following services: <b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent;">Accommodation</b>; 5, 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp; &nbsp;3 star hotels&nbsp;&nbsp;</div>]]>
</Inclusions>
<DEP DepartureCode="AN-17" Starts="04/19/2015" GBP="1458" EUR="1724" USD="2350" DISCOUNT="15%" />
<DEP DepartureCode="AN-18" Starts="04/22/2015" GBP="1558" EUR="1784" USD="2550" DISCOUNT="20%" />
<DEP DepartureCode="AN-19" Starts="04/25/2015" GBP="1558" EUR="1784" USD="2550" />
</TOUR>
<TOUR>
<Title><![CDATA[Miami &amp; the Dolphins tour]]></Title>
<Code>AE-20</Code>
<Duration>14</Duration>
<Start>Miami</Start>
<End>Miami</End>
<Inclusions>
<![CDATA[<div style="margin: 1px 0px; padding: 1px 0px; border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; text-align: justify; line-height: 19px; color: rgb(6, 119, 179);">The tour price&nbsp; cover the following services: <b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent;">Accommodation</b>; Mövenpick 5 star hotels&nbsp;&nbsp;</div>]]>
</Inclusions>
<DEP DepartureCode="AN-20" Starts="05/01/2016" GBP="1658" EUR="1924" USD="2450"/>
<DEP DepartureCode="AN-21" Starts="05/22/2016" GBP="1758" EUR="1984" USD="2650" DISCOUNT="20%" />
</TOUR>
</TOURS>
XML;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment