note 111310 added to function.xml-parse
| From: | omar dot technologies at gmail dot com | Date: | Tue, 05 Feb 2013 09:31:11 +0000 |
| Subject: | note 111310 added to function.xml-parse | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-193530@lists.php.net to get a copy of this message | ||
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');
----
Server IP: 69.147.83.199
Probable Submitter: 86.96.226.19
----
Manual Page -- http://php.net/manual/en/function.xml-parse.php
Edit -- https://master.php.net/note/edit/111310
Del: integrated -- https://master.php.net/note/delete/111310/integrated
Del: useless -- https://master.php.net/note/delete/111310/useless
Del: bad code -- https://master.php.net/note/delete/111310/bad+code
Del: spam -- https://master.php.net/note/delete/111310/spam
Del: non-english -- https://master.php.net/note/delete/111310/non-english
Del: in docs -- https://master.php.net/note/delete/111310/in+docs
Del: other reasons-- https://master.php.net/note/delete/111310
Reject -- https://master.php.net/note/reject/111310
Search -- https://master.php.net/manage/user-notes.php