This article gives an example of how PHP generates plist data. Share to everyone for your reference. Specifically as follows:
?
PHP/** * PropertyList class * Implements writing Apple Property List (. plist) XML and text files to an array. * * @author Jesus A. Alvarez <zydeco@namedfork.net>/function Plist_encode_text ($obj) {$plist = new Propertyli
St ($obj);
return $plist->text (); function Plist_encode_xml ($obj) {$plist = new propertylist ($obj); return $plist->xml ();} class PropertyList {Priv
Ate $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-&
Gt;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 (' int
Eger ', $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 = or
D (substr ($str, $i, 1)); if ($ch = = 0x22 | | $ch = = 0x5c) {//escape double quote, backslash $newstr. = ' \ n ' chr ($ch); $i + +;} else if ($ch >= 0x07 && $ch <= 0x0D) {//control characters with escape sequences $newstr. = ' \ \ ' substr (' Abtnvfr ', $ch-7
, 1);
$i + +; else if ($ch <) {//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 = | | | $ch = = 193) {//invalid encoding of ASCII C
Haracters $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. = ' {} '; $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 & amp; $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 Base64en Codeddata (){return Base64_encode ($this->data);} Public Function Hexencodeddata () {$len = strlen ($this->data); $hexstr = "for ($i = 0; $i < $len; $i + 4) $hexst R. = 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 ();
The Public Function iso8601date () {return gmdate (' y-m-d\th:i:s\z ', $this->dateval);}
?>
I hope this article will help you with your PHP programming.