Skip to content

Instantly share code, notes, and snippets.

@owise1
Created September 12, 2014 23:27
Show Gist options
  • Save owise1/6ef8c2837e1a5a79fda4 to your computer and use it in GitHub Desktop.
Save owise1/6ef8c2837e1a5a79fda4 to your computer and use it in GitHub Desktop.
Super Basic PHP XML Parser
/**
* Sometimes you don't want a giant library to deal with simple XML
*
* @param $tag - the XML tag you want the contents of
* - nested elements can be fetched using '>'. ex: 'Location>Address'
* @param $xmlStr - the XML
*/
function xmlGetItem($tag, $xmlStr){
$xmlStr = str_replace("\n", '', $xmlStr);
if(strstr($tag, '>')){
$pieces = split('>', $tag);
$exteriorTag = array_shift($pieces);
return xmlGetItem(join('>', $pieces), array_pop(xmlGetItem($exteriorTag, $xmlStr)));
} else {
$regex = '/<' . $tag . '>(.*?)<\/'. $tag .'>/';
preg_match_all($regex, $xmlStr, $matches);
return($matches[1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment