Created
September 12, 2014 23:27
-
-
Save owise1/6ef8c2837e1a5a79fda4 to your computer and use it in GitHub Desktop.
Super Basic PHP XML Parser
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
/** | |
* 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