php riff2

简易XML解析

 

//XML解析工具 //XML节点 class XMLNode { function XMLNode ($aa) { foreach ($aa as $k=>$v) $this->$k = $aa[$k]; } } function parseXML($data) { $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); // 遍历 XML 结构 foreach ($tags as $key=>$val) { if ($key == "COMMAND") { $molranges = $val; // each contiguous pair of array entries are the // lower and upper range for each molecule definition for ($i=0; $i < count($molranges); $i+=2) { $offset = $molranges[$i] + 1; $len = $molranges[$i + 1] - $offset; $tdb[] = parseMol(array_slice($values, $offset, $len)); } } else { continue; } } return $tdb[0]; } function parseMol($mvalues) { for ($i=0; $i < count($mvalues); $i++) { $mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"]; } return new XMLNode($mol); }

 

用法:

 

<Test>

<Test2>test2</Test2>

<Test3>test3</Test3>

</Test>

 

$db = parseXML($data);

$db->Test2 = test2

$db->Test3 = test3

 

 

 

UTF8子串截取

 

//截取utf8字符串 function utf8Substr($str, $from, $len) { return preg_replace('#^(?:[/x00-/x7F]|[/xC0-/xFF][/x80-/xBF]+){0,'.$from.'}'. '((?:[/x00-/x7F]|[/xC0-/xFF][/x80-/xBF]+){0,'.$len.'}).*#s', '$1',$str); }

 

你可能感兴趣的:(PHP,xml,function,command,each,tags)