欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP生成plist数据的方法

程序员文章站 2022-07-08 08:16:07
本文实例讲述了php生成plist数据的方法。分享给大家供大家参考。具体如下: 这段代码实现php数组转换为苹果plist xml或文本格式

本文实例讲述了php生成plist数据的方法。分享给大家供大家参考。具体如下:

这段代码实现php数组转换为苹果plist xml或文本格式

<?php
/**
 * propertylist class
 * implements writing apple property list (.plist) xml and text files from an array.
 *
 * @author jesus a. alvarez <zydeco@namedfork.net>
 */
function plist_encode_text ($obj) {
$plist = new propertylist($obj);
return $plist->text();
}
function plist_encode_xml ($obj) {
$plist = new propertylist($obj);
return $plist->xml();
}
class propertylist
{
private $obj, $xml, $text;
public function __construct ($obj) {
$this->obj = $obj;
}
private static function is_assoc ($array) {
return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
}
public function xml () {
if (isset($this->xml)) return $this->xml;
$x = new xmlwriter();
$x->openmemory();
$x->setindent(true);
$x->startdocument('1.0', 'utf-8');
$x->writedtd('plist', '-//apple//dtd plist 1.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;
}
public function text() {
if (isset($this->text)) return $this->text;
$text = '';
$this->textwritevalue($text, $this->obj);
$this->text = $text;
return $this->text;
}
private function xmlwritedict(xmlwriter $x, &$dict) {
$x->startelement('dict');
foreach($dict as $k => &$v) {
$x->writeelement('key', $k);
$this->xmlwritevalue($x, $v);
}
$x->endelement(); // dict
}
private function xmlwritearray(xmlwriter $x, &$arr) {
$x->startelement('array');
foreach($arr as &$v)
$this->xmlwritevalue($x, $v);
$x->endelement(); // array
}
private function xmlwritevalue(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("unsupported data type in plist ($v)", e_user_warning);
$x->writeelement('string', $v);
}
}
private function textwritevalue(&$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("unsupported data type in plist ($v)", e_user_warning);
$this->textwritestring($text, $v);
}
}
private function textwritestring(&$text, &$str) {
$oldlocale = setlocale(lc_ctype, "0");
if (ctype_alnum($str)) $text .= $str;
else $text .= '"' . $this->textencodestring($str) . '"';
setlocale(lc_ctype, $oldlocale);
}
private function textencodestring(&$str) {
$newstr = '';
$i = 0;
$len = strlen($str);
while($i < $len) {
$ch = ord(substr($str, $i, 1));
if ($ch == 0x22 || $ch == 0x5c) {
// escape double quote, backslash
$newstr .= '\\' . chr($ch);
$i++;
} else if ($ch >= 0x07 && $ch <= 0x0d ){
// control characters with escape sequences
$newstr .= '\\' . substr('abtnvfr', $ch - 7, 1);
$i++;
} else if ($ch < 32) {
// other non-printable characters escaped as unicode
$newstr .= sprintf('\u%04x', $ch);
$i++;
} else if ($ch < 128) {
// ascii printable
$newstr .= chr($ch);
$i++;
} else if ($ch == 192 || $ch == 193) {
// invalid encoding of ascii characters
$i++;
} else if (($ch & 0xc0) == 0x80){
// part of a lost multibyte sequence, skip
$i++;
} else if (($ch & 0xe0) == 0xc0) {
// u+0080 - u+07ff (2 bytes)
$u = (($ch & 0x1f) << 6) | (ord(substr($str, $i+1, 1)) & 0x3f);
$newstr .= sprintf('\u%04x', $u);
$i += 2;
} else if (($ch & 0xf0) == 0xe0) {
// u+0800 - u+ffff (3 bytes)
$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;
} else if (($ch & 0xf8) == 0xf0) {
// u+10000 - u+3ffff (4 bytes)
$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 {
// 5 and 6 byte sequences are not valid utf-8
$i++;
}
}
return $newstr;
}
private function textwritedict(&$text, &$dict, $indentlevel) {
if (count($dict) == 0) {
$text .= '{}';
return;
}
$text .= "{\n";
$indent = '';
$indentlevel++;
while(strlen($indent) < $indentlevel) $indent .= "\t";
foreach($dict as $k => &$v) {
$text .= $indent;
$this->textwritevalue($text, $k);
$text .= ' = ';
$this->textwritevalue($text, $v, $indentlevel);
$text .= ";\n";
}
$text .= substr($indent, 0, -1) . '}';
}
private function textwritearray(&$text, &$arr, $indentlevel) {
if (count($arr) == 0) {
$text .= '()';
return;
}
$text .= "(\n";
$indent = '';
$indentlevel++;
while(strlen($indent) < $indentlevel) $indent .= "\t";
foreach($arr as &$v) {
$text .= $indent;
$this->textwritevalue($text, $v, $indentlevel);
$text .= ",\n";
}
$text .= substr($indent, 0, -1) . ')';
}
}
class plistdata
{
private $data;
public function __construct($str) {
$this->data = $str;
}
public function base64encodeddata () {
return base64_encode($this->data);
}
public function hexencodeddata () {
$len = strlen($this->data);
$hexstr = '';
for($i = 0; $i < $len; $i += 4)
$hexstr .= bin2hex(substr($this->data, $i, 4)) . ' ';
return substr($hexstr, 0, -1);
}
}
class plistdate
{
private $dateval;
public function __construct($init = null) {
if (is_int($init))
$this->dateval = $init;
elseif (is_string($init))
$this->dateval = strtotime($init);
elseif ($init == null)
$this->dateval = time();
}
public function iso8601date() {
return gmdate('y-m-d\th:i:s\z', $this->dateval);
}
}
?>

希望本文所述对大家的php程序设计有所帮助。