note 111310 deleted from function.xml-parse by danbrown

From: Date: Tue, 05 Mar 2013 16:15:02 +0000
Subject: note 111310 deleted from function.xml-parse by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-193939@lists.php.net to get a copy of this message
Note Submitter: omar dot technologies at gmail dot com ---- I was trying to parse xml and for a weird reason, the site wasn't really returning an ".xml" extension and so ... the regular method wasn't working. What I did though, was something interesting and you can use this function if you want such a simple function. In my case, I didn't really care about the structure of the XML as much as the contents between the tags. Please note though, with this method, you only get the lowest level content, you can't get a count of the number of tags etc.. $xml = file_get_contents($link); (gets the xml as a string) foreach ($http_response_header as $header) (found this somewhere in the comments, used if it's not correctly utf-8'ed) { if (preg_match('#^Content-Type: text/xml; charset=(.*)#i', $header, $m)) { switch (strtolower($m[1])) { case 'utf-8': // do nothing break; case 'iso-8859-1': $xml = utf8_encode($xml); break; default: $xml = iconv($m[1], 'utf-8', $xml); } break; } } //THIS IS THE FUNCTION. It basically does a string search for the tag... and pulls out what's between the two tags... function out($xm,$tag) { $tag1 = '<'.$tag.'>'; $tag2 = '</'.$tag.'>'; $s = strpos($xm,$tag1); $j = strpos($xm,$tag2); $out = substr($xm,$s+strlen($tag1),$j-$s-strlen($tag2)+1); return $out; } //examples... lets say there was a tag "Sector".. <sector>something</sector>.. the below $sector will then contain the string "something" $sector = out($xml,'Sector'); $district = out($xml,'District'); $parcel = out($xml, 'Parcel');

« previous php.notes (#193939) next »