PHP生成plist数据的方法_PHP教程

WBOY
發布: 2016-07-13 09:50:40
原創
946 人瀏覽過

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

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

/**

* PropertyList class

* Implements writing Apple Property List (.plist) XML and text files from an array.

*

* @author Jesus A. Alvarez

*/

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 .= '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

$ch = ord(substr($str, $i, 1));

if ($ch == 0x22 || $ch == 0x5C) {

// escape double quote, backslash

$newstr .= '\\' . chr($ch);

$i++;

} else if ($ch >= 0x07 && $ch

// control characters with escape sequences

$newstr .= '\\' . substr('abtnvfr', $ch - 7, 1);

$i++;

} else if ($ch

// other non-printable characters escaped as unicode

$newstr .= sprintf('\U%04x', $ch);

$i++;

} else if ($ch

// 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)

$newstr .= sprintf('\U%04x', $u);

$i += 2;

} else if (($ch & 0xF0) == 0xE0) {

// U+0800 - U+FFFF (3 bytes)

$u = (($ch & 0x0F)

$newstr .= sprintf('\U%04x', $u);

$i += 3;

} else if (($ch & 0xF8) == 0xF0) {

// U+10000 - U+3FFFF (4 bytes)

$u = (($ch & 0x07)

$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)

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)

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

$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程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1017138.htmlTechArticlePHP生成plist数据的方法 本文实例讲述了PHP生成plist数据的方法。分享给大家供大家参考。具体如下: 这段代码实现PHP数组转换为苹果plist...
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板