Files
bobsleighphotos.com/_old/version_2/php/func_xml.php
2017-03-02 20:30:40 -06:00

136 lines
4.9 KiB
PHP
Executable File

<?php
#########################################################
# #
# XML functions #
# written by benjamin ramey #
# bsr@rameydesign.net #
# #
# best viewed with: #
# resolution = 1280x960 #
# tab = 4 spaces #
# indent = 8 spaces #
# #
########################################################################################## xml_parse_file
# parameters: path to file to parse
# returns: associative array of xml file data
function xml_parse_file($xml_file)
{
$xml = '';
$data = array();
if(file_exists($xml_file)) {
$xml = file_get_contents($xml_file);
if(empty($xml)) {
return(NULL);
}
}
else { return(NULL); }
$patt = "/^<\?xml([^\?]*)\?>/";
preg_match($patt,$xml,$matches);
$xml_specs = explode(' ',$matches[1]);
foreach($xml_specs as $spec) {
if(empty($spec)) { continue; }
list($name,$value) = explode('=',$spec);
$value = str_replace("'",'',$value);
$value = str_replace('"','',$value);
$data[$name] = $value;
}
$xml = substr($xml,strlen($matches[0]));
$xml_left = array($xml); // init xml heap
$tmp_refs[] =& $data; // init refs heap
$patt = "/^<(\w+)([^>]*)>/"; // pattern to get element at beginning of xml_left
while(!empty($xml_left)) {
$to_process = array_pop($xml_left); // $xml_left is a heap
$to_process = trim($to_process); // trim fore and aft spaces
$last_index = count($tmp_refs) - 1;
if(preg_match($patt,$to_process,$matches)) {
$element = $matches[1];
$parameters = isset($matches[2]) ? $matches[2] : '';
if(isset($tmp_refs[$last_index][$element])) {
if(is_array($tmp_refs[$last_index][$element])) {
if(isset($tmp_refs[$last_index][$element][0])) { // case: [biography]=>[0],[1]...[x]; $element = biography
array_push($tmp_refs[$last_index][$element],''); // endsup: [biography]=>[0],[1]...[x+1]
$tmp_index = count($tmp_refs[$last_index][$element]) - 1;
$tmp_refs[] =& $tmp_refs[$last_index][$element][$tmp_index];
}
else { // case: [biography]=>[paragraph]; $element = biography
$tmp_ary = $tmp_refs[$last_index][$element]; // endsup: [biography]=>[0]=>[paragraph]
$tmp_refs[$last_index][$element] = array($tmp_ary,''); // [1]=>''
$tmp_refs[] =& $tmp_refs[$last_index][$element][1];
}
}
else { // case: [biography]=>"string"; $element = biography
$tmp_refs[$last_index][$element] = array($tmp_refs[$last_index][$element],'');
$tmp_refs[] =& $tmp_refs[$last_index][$element][1]; // endsup: [biography]=>[0]=>"string"
} // [1]=>''
}
else {
$tmp_refs[$last_index][$element] = ''; // case: [biography]=> NULL; $element = biography
$tmp_refs[] =& $tmp_refs[$last_index][$element]; // endsup: [biography]=>""
}
if(!empty($parameters)) { // if there are parameters, add them as if they were imbedded elements
$pairs = explode(' ',$parameters);
$last_index = count($tmp_refs) - 1;
$tmp_refs[$last_index] = array(); // we can do this, because we know the last element added
// is always fresh and new
foreach($pairs as $pair) {
if(empty($pair)) { continue; }
list($name,$value) = explode('=',$pair);
$value = str_replace("'",'',$value);
$value = str_replace('"','',$value);
$tmp_refs[$last_index][$name] = $value;
}
}
$end_tag = strpos($to_process,"</$element>");
if($end_tag === FALSE) {
return("ERROR IN XML DOCUMENT, NO END TAG: $element\n\nto process: $to_process");
}
$element_len = strlen($matches[0]);
$contents = substr($to_process,$element_len,$end_tag - $element_len);
if($extra = substr($to_process,$end_tag + strlen("</$element>"))) {
array_push($xml_left,$extra);
}
else {
array_push($xml_left,'!~~END~~!');
}
array_push($xml_left,$contents);
}
else if($to_process != '!~~END~~!') { // no pattern match, but it's not the end of an embedded element
$tmp_refs[$last_index] = $to_process; // so we must have content to assign, so assign it--genius!
array_pop($tmp_refs);
}
else { // we've reached the end of an embedded element, so jump back a level
$tmp = array_pop($tmp_refs);
}
}
return($data);
}
########################################################################################## xml_force_array
# parameters: path to file to parse
# returns: associative array of xml file data
function xml_force_array($element)
{
$array = array();
if(is_array($element)) {
if(isset($element[0])) { $array = $element; }
else { array_push($array,$element); }
}
else { array_push($array,$element); }
return($array);
}
?>