php教程 PHP源码 PHP plist 生成器

PHP plist 生成器

May 25, 2016 pm 05:14 PM

PHP数组转换为苹果plist XML或文本格式                         

1. [代码][PHP]代码

    <?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(&#39;1.0&#39;, &#39;UTF-8&#39;);
    $x->writeDTD(&#39;plist&#39;, &#39;-//Apple//DTD PLIST 1.0//EN&#39;, &#39;http://www.apple.com/DTDs/PropertyList-1.0.dtd&#39;);
    $x->startElement(&#39;plist&#39;);
    $x->writeAttribute(&#39;version&#39;, &#39;1.0&#39;);
    $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 = &#39;&#39;;
    $this->textWriteValue($text, $this->obj);
    $this->text = $text;
    return $this->text;
    }
     
    private function xmlWriteDict(XMLWriter $x, &$dict) {
    $x->startElement(&#39;dict&#39;);
    foreach($dict as $k => &$v) {
    $x->writeElement(&#39;key&#39;, $k);
    $this->xmlWriteValue($x, $v);
    }
    $x->endElement(); // dict
    }
     
    private function xmlWriteArray(XMLWriter $x, &$arr) {
    $x->startElement(&#39;array&#39;);
    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(&#39;integer&#39;, $v);
    elseif (is_float($v) || is_real($v) || is_double($v))
    $x->writeElement(&#39;real&#39;, $v);
    elseif (is_string($v))
    $x->writeElement(&#39;string&#39;, $v);
    elseif (is_bool($v))
    $x->writeElement($v?&#39;true&#39;:&#39;false&#39;);
    elseif (PropertyList::is_assoc($v))
    $this->xmlWriteDict($x, $v);
    elseif (is_array($v))
    $this->xmlWriteArray($x, $v);
    elseif (is_a($v, &#39;PlistData&#39;))
    $x->writeElement(&#39;data&#39;, $v->base64EncodedData());
    elseif (is_a($v, &#39;PlistDate&#39;))
    $x->writeElement(&#39;date&#39;, $v->encodedDate());
    else {
    trigger_error("Unsupported data type in plist ($v)", E_USER_WARNING);
    $x->writeElement(&#39;string&#39;, $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?&#39;YES&#39;:&#39;NO&#39;;
    elseif (PropertyList::is_assoc($v))
    $this->textWriteDict($text, $v, $indentLevel);
    elseif (is_array($v))
    $this->textWriteArray($text, $v, $indentLevel);
    elseif (is_a($v, &#39;PlistData&#39;))
    $text .= &#39;<&#39; . $v->hexEncodedData() . &#39;>&#39;;
    elseif (is_a($v, &#39;PlistDate&#39;))
    $text .= &#39;"&#39; . $v->ISO8601Date() . &#39;"&#39;;
    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 .= &#39;"&#39; . $this->textEncodeString($str) . &#39;"&#39;;
    setlocale(LC_CTYPE, $oldlocale);
    }
     
    private function textEncodeString(&$str) {
    $newstr = &#39;&#39;;
    $i = 0;
    $len = strlen($str);
    while($i < $len) {
    $ch = ord(substr($str, $i, 1));
    if ($ch == 0x22 || $ch == 0x5C) {
    // escape double quote, backslash
    $newstr .= &#39;\\&#39; . chr($ch);
    $i++;
    } else if ($ch >= 0x07 && $ch <= 0x0D ){
    // control characters with escape sequences
    $newstr .= &#39;\\&#39; . substr(&#39;abtnvfr&#39;, $ch - 7, 1);
    $i++;
    } else if ($ch < 32) {
    // other non-printable characters escaped as unicode
    $newstr .= sprintf(&#39;\U%04x&#39;, $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(&#39;\U%04x&#39;, $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(&#39;\U%04x&#39;, $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(&#39;\U%04x&#39;, $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 .= &#39;{}&#39;;
    return;
    }
    $text .= "{\n";
    $indent = &#39;&#39;;
    $indentLevel++;
    while(strlen($indent) < $indentLevel) $indent .= "\t";
    foreach($dict as $k => &$v) {
    $text .= $indent;
    $this->textWriteValue($text, $k);
    $text .= &#39; = &#39;;
    $this->textWriteValue($text, $v, $indentLevel);
    $text .= ";\n";
    }
    $text .= substr($indent, 0, -1) . &#39;}&#39;;
    }
     
    private function textWriteArray(&$text, &$arr, $indentLevel) {
    if (count($arr) == 0) {
    $text .= &#39;()&#39;;
    return;
    }
    $text .= "(\n";
    $indent = &#39;&#39;;
    $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) . &#39;)&#39;;
    }
    }
     
    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 = &#39;&#39;;
    for($i = 0; $i < $len; $i += 4)
    $hexstr .= bin2hex(substr($this->data, $i, 4)) . &#39; &#39;;
    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(&#39;Y-m-d\TH:i:s\Z&#39;, $this->dateval);
    }
    }
    ?>
로그인 후 복사

                   

                   

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)