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 相关文章推荐
实现分十页分向前十页向后十页的处理
Oct 09 PHP
BBS(php &amp; mysql)完整版(二)
Oct 09 PHP
php下判断数组中是否存在相同的值array_unique
Mar 25 PHP
php快速url重写 更新版[需php 5.30以上]
Apr 20 PHP
PHP setcookie设置Cookie用法(及设置无效的问题)
Jul 13 PHP
xml在joomla表单中的应用详解分享
Jul 19 PHP
php中定时计划任务的实现原理
Jan 08 PHP
基于php socket(fsockopen)的应用实例分析
Jun 02 PHP
深入mysql_fetch_row()与mysql_fetch_array()的区别详解
Jun 05 PHP
php对二维数组进行相关操作(排序、转换、去空白等)
Nov 04 PHP
PHP的Yii框架的常用日志操作总结
Dec 08 PHP
详解PHP PDO简单教程
May 28 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
基于HBase Thrift接口的一些使用问题及相关注意事项的详解
2013/06/03 PHP
PHP调试及性能分析工具Xdebug详解
2017/02/09 PHP
YII框架中使用memcache的方法详解
2017/08/02 PHP
php中输出json对象的值(实现方法)
2018/03/07 PHP
PHP进阶学习之依赖注入与Ioc容器详解
2019/06/19 PHP
javascript基础之查找元素的详细介绍(访问节点)
2013/07/05 Javascript
js检测浏览器版本、核心、是否移动端示例
2014/04/24 Javascript
Jquery的each里用return true或false代替break或continue
2014/05/21 Javascript
javascript实现分栏显示小技巧附图
2014/10/13 Javascript
使用AngularJS编写较为优美的JavaScript代码指南
2015/06/19 Javascript
JavaScript生成SQL查询表单的方法
2015/08/13 Javascript
JavaScript中各种引用类型的常用操作方法小结
2016/05/05 Javascript
AngularJS入门教程之过滤器用法示例
2016/11/02 Javascript
vue使用watch 观察路由变化,重新获取内容
2017/03/08 Javascript
js手机号4位显示空格,银行卡每4位显示空格效果
2017/03/23 Javascript
ES6使用let命令更简单的实现块级作用域实例分析
2017/03/31 Javascript
node.js操作mongodb简单示例分享
2017/05/25 Javascript
微信小程序商品详情页规格属性选择示例代码
2017/10/30 Javascript
vue 实现websocket发送消息并实时接收消息
2019/12/09 Javascript
[01:00:14]DOTA2-DPC中国联赛 正赛 Ehome vs Elephant BO3 第二场 2月28日
2021/03/11 DOTA
python在多玩图片上下载妹子图的实现代码
2013/08/13 Python
从零学Python之入门(三)序列
2014/05/25 Python
Python使用xlrd模块操作Excel数据导入的方法
2015/05/26 Python
flask框架视图函数用法示例
2018/07/19 Python
对django layer弹窗组件的使用详解
2019/08/31 Python
python GUI库图形界面开发之PyQt5选项卡控件QTabWidget详细使用方法与实例
2020/03/01 Python
澳大利亚最好的厨具店:Kitchen Warehouse
2018/03/13 全球购物
印尼第一大家居、生活和家具电子商务:Ruparupa
2019/11/25 全球购物
关于安全的标语
2014/06/10 职场文书
大学生自荐材料范文
2014/12/30 职场文书
质量保证书
2015/01/17 职场文书
男人帮观后感
2015/06/18 职场文书
《珍珠鸟》教学反思
2016/02/16 职场文书
学校就业保障协议书
2019/06/24 职场文书
python制作图形界面的2048游戏, 基于tkinter
2021/04/06 Python
python numpy中setdiff1d的用法说明
2021/04/22 Python