Created
April 23, 2016 08:38
-
-
Save ahgood/acbfccfd6f21b6dbbb0210ab69c22995 to your computer and use it in GitHub Desktop.
//Modified from sample code: http://www.hackingwithphp.com/12/3/3/searching-and-filtering-with-xpath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$time_start = microtime(true); //record the execution time | |
$xml = simplexml_load_file('data.xml'); | |
$country = ''; | |
$city = ''; | |
$search = ''; | |
if (isset($_GET["search"])) { | |
$search = trim(htmlspecialchars($_GET["search"])); | |
} | |
if (isset($_GET["country"])) { | |
$country = trim(htmlspecialchars($_GET["country"])); | |
$country = '[Country="' . $country . '"]'; | |
} | |
if (isset($_GET["city"])) { | |
$city = trim(htmlspecialchars($_GET["city"])); | |
$city = '[City="' . $city . '"]'; | |
} | |
$hotels = $xml->xpath('/Root/Hotel/HotelInfo' . $country . $city); | |
foreach($hotels as $hotel) { | |
if ($search !== '') { | |
$hotel_str = strip_tags($hotel->asXML()); | |
if (strpos($hotel_str, $search) !== false) { | |
var_dump(json_encode($hotel)); | |
} | |
} else { | |
echo "Hotel: {$hotel->CompanyName}<br />"; | |
} | |
} | |
echo '<br />Total execution time in seconds: ' . (microtime(true) - $time_start); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment