note 65332 added to function.xml-parse-into-struct

From: Date: Fri, 28 Apr 2006 19:15:27 +0000
Subject: note 65332 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-110036@lists.php.net to get a copy of this message
I needed a very simple parser for a set of name-value pairs to be stored in a single database field. I started with the mold example, paired it down, picked up the "id" attributes. Perhaps it will be useful for someone else. <?php /* simple conversion for name-value fields */ $xmlInput = "<?xml version=\"1.0\"?> <mcw_settings> <field id=\"imageAlign\">left</field> <field id=\"caption\">What a nice picture.</field> </mcw_settings>"; $desiredResult = array ( 'imageAlign' => "left", 'caption' => "What a nice picture." ); function parseFields ($data) { // read the XML database of fields $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, $data, $values, $tags); xml_parser_free($parser); $fields = null; // loop through the structures $fieldIndices = $tags['field']; for ( $i=0; $i < count($fieldIndices); $i++ ) { $fieldInfo = $values[$fieldIndices[$i]]; $fields[$fieldInfo['attributes']['id']] = $fieldInfo['value']; } return $fields; } $test = parseFields($xmlInput); echo "** Result:\n"; print_r($test); ?> ---- Server IP: 64.71.164.2 Probable Submitter: 72.164.207.3 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php Edit -- http://master.php.net/note/edit/65332 Del: integrated -- http://master.php.net/note/delete/65332/integrated Del: useless -- http://master.php.net/note/delete/65332/useless Del: bad code -- http://master.php.net/note/delete/65332/bad+code Del: spam -- http://master.php.net/note/delete/65332/spam Del: non-english -- http://master.php.net/note/delete/65332/non-english Del: in docs -- http://master.php.net/note/delete/65332/in+docs Del: other reasons-- http://master.php.net/note/delete/65332 Reject -- http://master.php.net/note/reject/65332 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#110036) next »