note 59017 added to function.xml-parse
| From: | jwross at fastmail dot fm | Date: | Thu, 24 Nov 2005 01:40:34 +0000 |
| Subject: | note 59017 added to function.xml-parse | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99006@lists.php.net to get a copy of this message | ||
Here is an /updated/ version of the wonderful google maps parser that jdennis at gmail dot com
shared with us. The Parser works perfectly, except that in multiple calls, the $charsFound variable
is not reset, causing havoc in the array! The changes are in function parseSegments. This parser
adds much needed functionality to the use of the Google Maps API on a personal (or corporate)
website.
Thanks jdennis!
<?
$mapsurl = "http://maps.google.com/maps";
$source_address = "595 Market St. San Francisco, CA 94105";
$destination_address = "14 Wall St. New York, NY 10005";
/**
* These variables are shared among the xml parsers and reset
* when parseSegments is called
*/
$mapOfSegments = array();
$segsFound = 0;
$characterData = array();
$charsFound = 0;
$map = array();
/* end of special xml variables */
$directions = getDirections($source_address, $destination_address);
print "The directions structure:<pre>"; print_r($directions); print
"</pre>";
function getDirections($source_address, $destination_address) {
global $mapsurl;
global $map;
$source_address = preg_replace("| |", "%20", $source_address);
$destination_address = preg_replace("| |", "%20", $destination_address);
$data = file("$mapsurl?saddr=$source_address&daddr=$destination_address");
// Line 3 contains the xml structure with the directions on it
$segments = $data[2];
$data = null;
$segments = preg_replace("|^.*<segments|", "<segments", $segments);
$segments = preg_replace("|</segments>.*$|", "", $segments);
$segments = preg_replace("|<b>|", "", $segments);
$segments = preg_replace("|</b>|", "", $segments);
parseSegments($segments);
return $map;
}
function parseSegments($segments) {
global $segsFound;
global $mapOfSegments;
global $charsFound;
global $characterData;
global $map;
// reset elements
$segsFound = 0;
$mapOfSegments = array();
$charsFound = 0;
$characterData = array();
$map = array();
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterDataFunction");
if (!xml_parse($xml_parser, $segments)) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
xml_parser_free($xml_parser);
$map = $mapOfSegments[0];
$map['STEPS'] = array_splice($mapOfSegments, 1, count($mapOfSegments));
// Map character data into array
for($i=0; $i<sizeof($map['STEPS']); $i++) {
$map['STEPS'][$i]['TEXT'] = $characterData[$i]['TEXT'];
}
}
function startElement($parser, $name, $attrs) {
global $depth;
global $segsFound;
global $mapOfSegments;
$mapOfSegments[$segsFound]['ID'] = $attrs['ID'];
$mapOfSegments[$segsFound]['POINTINDEX'] = $attrs['POINTINDEX'];
$mapOfSegments[$segsFound]['METERS'] = $attrs['METERS'];
$mapOfSegments[$segsFound]['SECONDS'] = $attrs['SECONDS'];
$mapOfSegments[$segsFound++]['DISTANCE'] = $attrs['DISTANCE'];
}
function endElement($parser, $name) {
global $charsFound;
$charsFound++;
}
function characterDataFunction($parser, $string) {
global $charsFound;
global $characterData;
$characterData[$charsFound]['TEXT'] = $string;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59017
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59017&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59017&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59017&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59017&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59017&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59017&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59017&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59017&report=yes
Search -- http://master.php.net/manage/user-notes.php