PHP生成plist数据的方法

   2015-06-23 0
核心提示:这篇文章主要介绍了PHP生成plist数据的方法,可实现PHP数组转换为苹果plist XML或文本格式的功能,需要的朋友可以参考下

本文实例讲述了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 生成
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • php-fpm进程管理的三种模式 phpfpm子进程
    php-fpm进程管理的三种模式 phpfpm子进程
    php-fpm解读-进程管理的三种模式—程序媛大丽标明转载以示尊重 感谢原作者的分享。php-fpm进程管理一共有三种模式:ondemand、static、dynamic,我们可以在同一个fpm的master配置三种模式,看下图1。php-fpm的工作模式和nginx类似,都是一个master,多个worke
    03-08
  • nginx和php-fpm 是使用 tcp socket 还是 unix s
    tcp socket允许通过网络进程之间的通信,也可以通过loopback进行本地进程之间通信。unix socket允许在本地运行的进程之间进行通信。分析从上面的图片可以看,unix socket减少了不必要的tcp开销,而tcp需要经过loopback,还要申请临时端口和tcp相关资源。但是
    03-08
  • [PHP8] 我参加了PHP8工程师认证初学者考试beta考试
    [PHP8] 我参加了PHP8工程师认证初学者考试beta
    前几天,2022/08/05,PHP工程师认证机构PHP8 技术员认证初级考试宣布实施考试将于 2023 年春季开始。和 beta 测试完成于 2022/09/11所以我收到了。一般社团法人BOSS-CON JAPAN(代表理事:Tadashi Yoshimasa,地点:东京都世田谷区,以下简称“BOSS-CON JAPAN
    03-08
  • 将 PHP Insights 放入旧版 PJ 不是很好吗?谈论
    将 PHP Insights 放入旧版 PJ 不是很好吗?谈论
    介绍在最近的PHP系统开发中,感觉故事在理所当然包含静态分析工具的前提下进行。我的周围现有代码很脏,我很久以前安装了工具,但几乎没有检查已经观察到许多这样的案例。 (这是小说。而不是像 0 或 100 这样不允许单行错误的静态分析,一点一点,逐渐我想介
    03-08
  • PHP基于elasticsearch全文搜索引擎的开发 php使
    1.概述:全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称 Elastic)是目前全文搜索引擎的首选。Elastic 的底层是开源库 Lucene。但是,你没法直接用 Lucene,必须自己写代码去调用它的接口。Elastic 是 Lucene 的封装,提供了 REST API 的操作接
    02-09
  • php视图操作
    一、视图的基本介绍         视图是虚拟的表。与包含数据的表不一样,视图只包含使用时动态检索数据的查询。        使用视图需要MySQL5及以后的版本支持。        下面是视图的一些常见应用:        重用SQL语句;        简化复杂的S
    02-09
  • php中图像处理的常用函数 php图形图像处理技术
    php中图像处理的常用函数 php图形图像处理技术
    1.imagecreate()函数imagecreate()函数是基于一个调色板的画布。?php $im = imagecreate(200,80);                //创建一个宽200,高80的画布。$white = imagecolorallocate($im,225,35,180);     //设置画布的背景颜色imagegif($im);
    02-09
  • PHP安全之webshell和后门检测
    PHP安全之webshell和后门检测
    基于PHP的应用面临着各种各样的攻击:XSS:对PHP的Web应用而言,跨站脚本是一个易受攻击的点。攻击者可以利用它盗取用户信息。你可以配置Apache,或是写更安全的PHP代码(验证所有用户输入)来防范XSS攻击SQL注入:这是PHP应用中,数据库层的易受攻击点。防范
    02-09
  • php使用时间戳保存时间的意义 PHP获取时间戳
    时间戳记录的是格林尼治时间,使用date格式化的时候会根据你程序设置的不同时区显示不同的时间。如果使用具体时间,则还需要进行多一步转换。
    02-09
  • PHP 获取提交表单数据方法
    PHP $_GET 和 $_POST变量是用来获取表单中的信息的,比如用户输入的信息。PHP表单操作在我们处理HTML表单和PHP表单时,我们要记住的重要一点是:HTML页面中的任何一个表单元素都可以自动的用于PHP脚本:表单举例: htmlbodyform action="welcome.php" method
    02-09
点击排行