note 57369 added to function.xml-parse
| From: | tim at alloutinteraction dot com | Date: | Sat, 01 Oct 2005 14:29:45 +0000 |
| Subject: | note 57369 added to function.xml-parse | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-96141@lists.php.net to get a copy of this message | ||
I wanted to create a really simple XML parser, but I found the array management in xml_parse a bit
daunting. So I flattened my XML and parsed it using string matching. It wouldn't be difficult
to add xml depth (of 2 plus levels) by modifying the parsedXML array.
<?
// here's the raw html
$xmlRaw="<order>Order data</order><label>Label
data</label><control>123</control>";
// here are the xml field names
$xmlFieldNames=array("order", "label", "control");
// for each xml field...
foreach ($xmlFieldNames as $xmlField) {
if(strpos($xmlRaw,$xmlField)!==false){
// I've broken 1 single line into 4 for display purposes
$parsedXML[$xmlField]=substr($xmlRaw,
strpos($xmlRaw,"<$xmlField>")+strlen("<$xmlField>"),
strpos($xmlRaw,"</$xmlField>")-strlen("<$xmlField>")
-strpos($xmlRaw,"<$xmlField>"));
}
}
print_r($parsedXML);
// prints: Array ( [order] => Order data [label] => Label data [control] => 123 )
?>
Hope you find this useful (coded it while ill in bed with streaming cold, but felt much better
afterwards!)
Tim (a lazy coder)
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57369
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57369&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57369&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57369&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57369&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57369&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57369&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57369&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57369&report=yes
Search -- http://master.php.net/manage/user-notes.php