PHP plist Data Generation Method

Source: Internet
Author: User
Tags 04x 0xc0 control characters printable characters unsupported

PHP plist Data Generation Method

This example describes how to generate plist data in PHP. Share it with you for your reference. The details are as follows:

This code implements PHP array conversion to Apple plist XML or text format

?

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

<? 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 )))));

}

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 + 3 FFFF (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 );

}

}

?>

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.