PHP generates plist data, PHP generates plist_php tutorials

Source: Internet
Author: User
Tags 04x 0xc0 chr control characters printable characters unsupported

PHP generates plist data, PHP generates plist


The example in this paper describes how PHP generates plist data. Share to everyone for your reference. Specific as follows:

This code implements the PHP array to convert to Apple plist XML or text format

<? php/** * PropertyList class * Implements writing Apple Property List (. plist) XML and the text files from an array. * * @author Jesus A. Alvarez
 
  
 */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;p ublic function __construct ($obj) {$this->obj = $obj;} private static function Is_assoc ($array) {return (Is_array ($array) && 0!== count (Array_diff_key ($array, Array_k Eys (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 (subst R ($str, $i, 1)); if ($ch = = 0x22 | | $ch = = 0x5C) {//escape double quote, backslash$newstr. = ' \ \ '. chr ($ch); $i + +;} else I F ($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 I F ($ch <) {//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 is 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 =& Gt & $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;p ublic 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;p ublic 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);}}? >
 

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1017889.html www.bkjia.com true http://www.bkjia.com/PHPjc/1017889.html techarticle PHP generates plist data, PHP generates plist This example describes how PHP generates plist data. Share to everyone for your reference. As follows: This code implements the PHP array conversion ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.