本文实例讲述了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、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!