PHP生成plist数据的方法


Posted in PHP onJune 16, 2015

本文实例讲述了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程序设计有所帮助。

PHP 相关文章推荐
第1次亲密接触PHP5(1)
Oct 09 PHP
php 生成WML页面方法详解
Aug 09 PHP
php curl 伪造IP来源的实例代码
Nov 01 PHP
PHP中数组定义的几种方法
Sep 01 PHP
php使用ICQ网关发送手机短信
Oct 30 PHP
测试php函数的方法
Nov 13 PHP
PHP执行Curl时报错提示CURL ERROR: Recv failure: Connection reset by peer的解决方法
Jun 26 PHP
PHP判断来访是搜索引擎蜘蛛还是普通用户的代码小结
Sep 14 PHP
PHP图形计数器程序显示网站用户浏览量
Jul 20 PHP
Thinkphp框架中D方法与M方法的区别
Dec 23 PHP
PHP类与对象后期静态绑定操作实例详解
Dec 20 PHP
PHP7数组的底层实现示例
Aug 25 PHP
php动态绑定变量的用法
Jun 16 #PHP
php实现在服务器端调整图片大小的方法
Jun 16 #PHP
PHP正则验证Email的方法
Jun 15 #PHP
PHP实现通过正则表达式替换回调的内容标签
Jun 15 #PHP
PHP检测用户语言的方法
Jun 15 #PHP
php实现求相对时间函数
Jun 15 #PHP
php数组随机排序实现方法
Jun 13 #PHP
You might like
PHP MemCached 高级缓存应用代码
2010/08/05 PHP
解析mysql中UNIX_TIMESTAMP()函数与php中time()函数的区别
2013/06/24 PHP
php实现约瑟夫问题的方法小结
2015/03/23 PHP
Symfony2学习笔记之控制器用法详解
2016/03/17 PHP
PHP JWT初识及其简单示例
2018/10/10 PHP
ASP.NET jQuery 实例4(复制TextBox的文本到本地剪贴板上)
2012/01/13 Javascript
JS获取节点的兄弟,父级,子级元素的方法
2014/01/09 Javascript
EasyUI中datagrid在ie下reload失败解决方案
2015/03/09 Javascript
全面解析bootstrap格子布局
2016/05/22 Javascript
AngularJs bootstrap搭载前台框架——准备工作
2016/09/01 Javascript
jquery 属性选择器(匹配具有指定属性的元素)
2016/09/06 Javascript
详解jQuery停止动画——stop()方法的使用
2016/12/14 Javascript
Angular使用操作事件指令ng-click传多个参数示例
2018/03/27 Javascript
Koa2微信公众号开发之本地开发调试环境搭建
2018/05/16 Javascript
微信小程序开发之点击按钮退出小程序的实现方法
2019/04/26 Javascript
jquery实现垂直手风琴导航栏
2020/02/18 jQuery
[02:47]DOTA2英雄基础教程 野性怒吼兽王
2013/12/05 DOTA
跟老齐学Python之永远强大的函数
2014/09/14 Python
python下10个简单实例代码
2017/11/15 Python
使用anaconda的pip安装第三方python包的操作步骤
2018/06/11 Python
Python基础教程之异常详解
2019/01/10 Python
调试Django时打印SQL语句的日志代码实例
2019/09/12 Python
Python打包模块wheel的使用方法与将python包发布到PyPI的方法详解
2020/02/12 Python
python数据库编程 Mysql实现通讯录
2020/03/27 Python
Python装饰器的应用场景代码总结
2020/04/10 Python
完美解决keras保存好的model不能成功加载问题
2020/06/11 Python
Python接口自动化系列之unittest结合ddt的使用教程详解
2021/02/23 Python
CSS3系列教程:背景图片(背景大小和多背景图) 应用说明
2012/12/19 HTML / CSS
简单html5代码获取地理位置
2014/03/31 HTML / CSS
教育专业自荐书范文
2013/12/17 职场文书
护士岗前培训自我评鉴
2014/02/28 职场文书
项目经理岗位职责范本
2015/04/01 职场文书
中学社团活动总结
2015/05/07 职场文书
高端收音机+蓝牙音箱,JBL TUNER FM带收音蓝牙音箱评测
2021/04/24 无线电
css3中transform属性实现的4种功能
2021/08/07 HTML / CSS
Python OpenCV形态学运算示例详解
2022/04/07 Python