PHP生成plist数据的方法
本文实例讲述了PHP生成plist数据的方法。分享给大家供大家参考。具体如下:
这段代码实现PHP数组转换为苹果plistXML或文本格式
<?PHP /** *PropertyListclass *ImplementswritingApplePropertyList(.plist)XMLandtextfilesfromanarray. * *@authorJesusA.Alvarez<zydeco@namedfork.net> */ functionplist_encode_text($obj){ $plist=newPropertyList($obj); return$plist->text(); } functionplist_encode_xml($obj){ $plist=newPropertyList($obj); return$plist->xml(); } classPropertyList { private$obj,$xml,$text; publicfunction__construct($obj){ $this->obj=$obj; } privatestaticfunctionis_assoc($array){ return(is_array($array)&&0!==count(array_diff_key($array,array_keys(array_keys($array))))); } publicfunctionxml(){ if(isset($this->xml))return$this->xml; $x=newXMLWriter(); $x->openMemory(); $x->setIndent(TRUE); $x->startDocument('1.0','UTF-8'); $x->writeDTD('plist','-//Apple//DTDPLIST1.0//EN','http://www.apple.com/DTDs/PropertyList-1.0.dtd'); $x->startElement('plist'); $x->writeAttribute('version','1.0'); $this->xmlWriteValue($x,$this->obj); $x->endElement();//plist $x->endDocument(); $this->xml=$x->outputMemory(); return$this->xml; } publicfunctiontext(){ if(isset($this->text))return$this->text; $text=''; $this->textWriteValue($text,$this->obj); $this->text=$text; return$this->text; } privatefunctionxmlWriteDict(XMLWriter$x,&$dict){ $x->startElement('dict'); foreach($dictas$k=>&$v){ $x->writeElement('key',$k); $this->xmlWriteValue($x,$v); } $x->endElement();//dict } privatefunctionxmlWriteArray(XMLWriter$x,&$arr){ $x->startElement('array'); foreach($arras&$v) $this->xmlWriteValue($x,$v); $x->endElement();//array } privatefunctionxmlWriteValue(XMLWriter$x,&$v){ if(is_int($v)||is_long($v)) $x->writeElement('integer',$v); elseif(is_float($v)||is_real($v)||is_double($v)) $x->writeElement('real',$v); elseif(is_string($v)) $x->writeElement('string',$v); elseif(is_bool($v)) $x->writeElement($v?'true':'false'); elseif(PropertyList::is_assoc($v)) $this->xmlWriteDict($x,$v); elseif(is_array($v)) $this->xmlWriteArray($x,$v); elseif(is_a($v,'PlistData')) $x->writeElement('data',$v->base64EncodedData()); elseif(is_a($v,'PlistDate')) $x->writeElement('date',$v->encodedDate()); else{ trigger_error("Unsupporteddatatypeinplist($v)",E_USER_WARNING); $x->writeElement('string',$v); } } privatefunctiontextWriteValue(&$text,&$v,$indentLevel=0){ if(is_int($v)||is_long($v)) $text.=sprintf("%d",$v); elseif(is_float($v)||is_real($v)||is_double($v)) $text.=sprintf("%g",$v); elseif(is_string($v)) $this->textWriteString($text,$v); elseif(is_bool($v)) $text.=$v?'YES':'NO'; elseif(PropertyList::is_assoc($v)) $this->textWriteDict($text,$v,$indentLevel); elseif(is_array($v)) $this->textWriteArray($text,$v,$indentLevel); elseif(is_a($v,'PlistData')) $text.='<'.$v->hexEncodedData().'>'; elseif(is_a($v,'PlistDate')) $text.='"'.$v->ISO8601Date().'"'; else{ trigger_error("Unsupporteddatatypeinplist($v)",E_USER_WARNING); $this->textWriteString($text,$v); } } privatefunctiontextWriteString(&$text,&$str){ $oldlocale=setlocale(LC_CTYPE,"0"); if(ctype_alnum($str))$text.=$str; else$text.='"'.$this->textEncodeString($str).'"'; setlocale(LC_CTYPE,$oldlocale); } privatefunctiontextEncodeString(&$str){ $newstr=''; $i=0; $len=strlen($str); while($i<$len){ $ch=ord(substr($str,$i,1)); if($ch==0x22||$ch==0x5C){ //escapedoublequote,backslash $newstr.='\\'.chr($ch); $i++; }elseif($ch>=0x07&&$ch<=0x0D){ //controlcharacterswithescapesequences $newstr.='\\'.substr('abtnvfr',$ch-7,1); $i++; }elseif($ch<32){ //othernon-printablecharactersescapedasunicode $newstr.=sprintf('\U%04x',$ch); $i++; }elseif($ch<128){ //asciiprintable $newstr.=chr($ch); $i++; }elseif($ch==192||$ch==193){ //invalidencodingofASCIIcharacters $i++; }elseif(($ch&0xC0)==0x80){ //partofalostmultibytesequence,skip $i++; }elseif(($ch&0xE0)==0xC0){ //U+0080-U+07FF(2bytes) $u=(($ch&0x1F)<<6)|(ord(substr($str,$i+1,1))&0x3F); $newstr.=sprintf('\U%04x',$u); $i+=2; }elseif(($ch&0xF0)==0xE0){ //U+0800-U+FFFF(3bytes) $u=(($ch&0x0F)<<12)|((ord(substr($str,$i+1,1))&0x3F)<<6)|(ord(substr($str,$i+2,1))&0x3F); $newstr.=sprintf('\U%04x',$u); $i+=3; }elseif(($ch&0xF8)==0xF0){ //U+10000-U+3FFFF(4bytes) $u=(($ch&0x07)<<18)|((ord(substr($str,$i+1,1))&0x3F)<<12)|((ord(substr($str,$i+2,1))&0x3F)<<6)|(ord(substr($str,$i+3,1))&0x3F); $newstr.=sprintf('\U%04x',$u); $i+=4; }else{ //5and6bytesequencesarenotvalidUTF-8 $i++; } } return$newstr; } privatefunctiontextWriteDict(&$text,&$dict,$indentLevel){ if(count($dict)==0){ $text.='{}'; return; } $text.="{\n"; $indent=''; $indentLevel++; while(strlen($indent)<$indentLevel)$indent.="\t"; foreach($dictas$k=>&$v){ $text.=$indent; $this->textWriteValue($text,$k); $text.='='; $this->textWriteValue($text,$v,$indentLevel); $text.=";\n"; } $text.=substr($indent,0,-1).'}'; } privatefunctiontextWriteArray(&$text,&$arr,$indentLevel){ if(count($arr)==0){ $text.='()'; return; } $text.="(\n"; $indent=''; $indentLevel++; while(strlen($indent)<$indentLevel)$indent.="\t"; foreach($arras&$v){ $text.=$indent; $this->textWriteValue($text,$v,$indentLevel); $text.=",\n"; } $text.=substr($indent,0,-1).')'; } } classPlistData { private$data; publicfunction__construct($str){ $this->data=$str; } publicfunctionbase64EncodedData(){ returnbase64_encode($this->data); } publicfunctionhexEncodedData(){ $len=strlen($this->data); $hexstr=''; for($i=0;$i<$len;$i+=4) $hexstr.=bin2hex(substr($this->data,$i,4)).''; returnsubstr($hexstr,0,-1); } } classPlistDate { private$dateval; publicfunction__construct($init=NULL){ if(is_int($init)) $this->dateval=$init; elseif(is_string($init)) $this->dateval=strtotime($init); elseif($init==NULL) $this->dateval=time(); } publicfunctionISO8601Date(){ returngmdate('Y-m-d\TH:i:s\Z',$this->dateval); } } ?>
希望本文所述对大家的php程序设计有所帮助。