PHP生成plist数据的方法_php技巧

本文实例讲述了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
, 生成
plist数据
php 生成plist、phpstorm生成方法注释、php生成150dpi图方法、u方法生成有index.php、smbios.plist 生成器,以便于您获取更多的相关知识。

时间: 2024-08-30 04:39:07

PHP生成plist数据的方法_php技巧的相关文章

PHP生成plist数据的方法

        本文实例讲述了PHP生成plist数据的方法.分享给大家供大家参考.具体如下:          这段代码实现PHP数组转换为苹果plist XML或文本格式 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 5

用PHP实现多服务器共享SESSION数据的方法_php技巧

PHP 实现多服务器共享 SESSION 数据 /google 的广告条--> 一.问题起源 稍大一些的网站,通常都会有好几个服务器,每个服务器运行着不同功能的模块,使用不同的二级域名,而一个整体性强的网站,用户系统是统一的,即一套用户名.密码在整个网站的各个模块中都是可以登录使用的.各个服务器共享用户数据是比较容易实现的,只需要在后端放个数据库服务器,各个服务器通过统一接口对用户数据进行访问即可.但还存在一个问题,就是用户在这个服务器登录之后,进入另一个服务器的别的模块时,仍然需要重新登录,这

PHP生成和获取XML格式数据的方法_php技巧

本文实例讲述了PHP生成和获取XML格式数据的方法.分享给大家供大家参考,具体如下: 在做数据接口时,我们通常要获取第三方数据接口或者给第三方提供数据接口,而这些数据格式通常是以XML或者JSON格式传输,这里将介绍如何使用PHP生成XML格式数据供第三方调用以及如何获取第三方提供的XML数据. 生成XML格式数据 我们假设系统中有一张学生信息表student,需要提供给第三方调用,并有id,name,sex,age分别记录学生的姓名.性别.年龄等信息. CREATE TABLE `studen

PHP实现获取并生成数据库字典的方法_php技巧

本文实例讲述了PHP实现获取并生成数据库字典的方法.分享给大家供大家参考,具体如下: <?php /** * 生成mysql数据字典 */ header("Content-type:text/html;charset=utf-8"); // 配置数据库 $database = array(); $database['DB_HOST'] = 'localhost'; $database['DB_NAME'] = 'test'; $database['DB_USER'] = 'roo

PHP使用ob_start生成html页面的方法_php技巧

本文实例讲述了PHP使用ob_start生成html页面的方法.分享给大家供大家参考.具体方法分析如下: ob_start([string output_callback])- 打开输出缓冲区 所有的输出信息不在直接发送到浏览器,而是保存在输出缓冲区里面,可选得回调函数用于处理输出结果信息. ob_end_flush - 结束(发送)输出缓冲区的内容,关闭输出缓冲区 使用output control 函数可以让自由控制脚本中数据的输出,这在我们想在header之前输出时很有用. 复制代码 代码如

Linux平台php命令行程序处理管道数据的方法_php技巧

本文实例讲述了Linux平台php命令行程序处理管道数据的方法.分享给大家供大家参考,具体如下: linux下有一个强大的命令|(管道提示符).它的作用是将前一个命令的结果交给后一条命令并作为后一条命令的输入.而linux下的大多数命令 也都支持这种方式.可是当笔者写完一个php的命令行小程序以后,对于怎样获得前一个命令的结果却陷入了僵局.难道php不支持这样的操作? 于是又开始问google大叔.找来找去,都是说php的命令行模式是怎么回事儿,也没有和我想知道的问题相关的资料.难道是俺的关键字

php实现根据字符串生成对应数组的方法_php技巧

本文实例讲述了php实现根据字符串生成对应数组的方法,是比较实用的技巧.分享给大家供大家参考.具体方法如下: 先看看如下示例: <?php $config = array( 'project|page|index' => 'content', 'project|page|nav' => array( array( 'image' => '1.jpg', 'name' => 'home' ), array( 'image' => '2.jpg', 'name' =>

php生成RSS订阅的方法_php技巧

本文实例讲述了php生成RSS订阅的方法.分享给大家供大家参考.具体分析如下: RSS(简易信息聚合,也叫聚合内容)是一种描述和同步网站内容的格式.RSS可以是以下三个解释的其中一个: Really Simple Syndication:RDF (Resource Description Framework) Site Summary: Rich Site Summary.但其实这三个解释都是指同一种Syndication的技术.RSS目前广泛用于网上新闻频道,blog和wiki.使用RSS订阅

php通过curl添加cookie伪造登陆抓取数据的方法_php技巧

本文实例讲述了php通过curl添加cookie伪造登陆抓取数据的方法.分享给大家供大家参考,具体如下: 有的网页必须登陆才能看到,这个时候想要抓取信息必须在header里面传递cookie值才能获取 1.首先登陆网站,打开firebug就能看到对应的cookie把这些cookie拷贝出来就能使用了 2. <?php header("Content-type:text/html;Charset=utf8"); $ch =curl_init(); curl_setopt($ch,C