-
Notifications
You must be signed in to change notification settings - Fork 0
/
php.go
334 lines (255 loc) · 5.5 KB
/
php.go
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package php
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"fmt"
"hash/crc32"
"html"
"io/ioutil"
"net/url"
"os"
"strings"
"time"
"unicode/utf8"
)
const GO_TIME_LAYOUT = "2006-01-02 15:04:05"
func Time() int64 {
return time.Now().Unix()
}
func Date(format string, timestamp int64) string {
if len(format) > 0 {
return time.Unix(timestamp, 0).Format(format)
}
return time.Unix(timestamp, 0).Format(GO_TIME_LAYOUT)
}
func Strtotime(datetime string, format ...string) int64 {
if len(format) > 0 {
t, e := time.Parse(format[0], datetime)
if e != nil {
return 0
}
return t.Unix()
}
t, e := time.Parse(GO_TIME_LAYOUT, datetime)
if e != nil {
return 0
}
return t.Unix()
}
func Sleep(sec int64) {
time.Sleep(time.Duration(sec) * time.Second)
}
func Usleep(usec int64) {
time.Sleep(time.Duration(usec) * time.Microsecond)
}
/////////////
// strings //
/////////////
func Strlen(str string) int {
return len(str)
}
func Mb_strlen(str string) int {
return utf8.RuneCountInString(str)
}
func Str_replace(search, replace, subject string, count ...int) string {
if len(count) > 0 {
return strings.Replace(subject, search, replace, count[0])
}
return strings.Replace(subject, search, replace, -1)
}
func Strtoupper(str string) string {
return strings.ToUpper(str)
}
func Strtolower(str string) string {
return strings.ToLower(str)
}
func Ucwords(str string) string {
return strings.Title(str)
}
func Substr(str string, start uint, length int) string {
if start < 0 || length < -1 {
return str
}
switch {
case length == -1:
return str[start:]
case length == 0:
return ""
}
end := int(start) + length
if end > len(str) {
end = len(str)
}
return str[start:end]
}
func Strrev(str string) string {
new_str := ""
ll := len(str)
for ; ll > 0; ll-- {
new_str += str[ll-1 : ll]
}
return new_str
}
func Trim(str string, charlist ...string) string {
def := "\t\n\r\\0\\x0B "
if len(charlist) > 0 {
def = charlist[0]
}
return strings.Trim(str, def)
}
func Ltrim(str string, charlist ...string) string {
def := "\t\n\r\\0\\x0B "
if len(charlist) > 0 {
def = charlist[0]
}
return strings.TrimLeft(str, def)
}
func Rtrim(str string, charlist ...string) string {
def := "\t\n\r\\0\\x0B "
if len(charlist) > 0 {
def = charlist[0]
}
return strings.TrimRight(str, def)
}
func Explode(delimiter, str string) []string {
return strings.Split(str, delimiter)
}
func Implode(glue string, arr []string) string {
var buffer bytes.Buffer
ll := len(arr)
for _, str := range arr {
buffer.WriteString(str)
ll--
if ll > 0 {
buffer.WriteString(glue)
}
}
return buffer.String()
}
func Chr(code int) string {
return string(code)
}
func Ord(char string) int {
r, _ := utf8.DecodeRune([]byte(char))
return int(r)
}
func Md5(str string) string {
hash := md5.New()
hash.Write([]byte(str))
return hex.EncodeToString(hash.Sum(nil))
}
func Md5_file(filename string) string {
data, _ := ioutil.ReadFile(filename)
hash := md5.New()
hash.Write([]byte(data))
return hex.EncodeToString(hash.Sum(nil))
}
func Sha1(str string) string {
hash := sha1.New()
hash.Write([]byte(str))
return hex.EncodeToString(hash.Sum(nil))
}
func Sha1_file(filename string) string {
data, _ := ioutil.ReadFile(filename)
hash := sha1.New()
hash.Write([]byte(data))
return hex.EncodeToString(hash.Sum(nil))
}
func Base64_encode(str string) string {
return base64.StdEncoding.EncodeToString([]byte(str))
}
func Base64_decode(str string) string {
if len(str)%4 == 2 {
str += "=="
}
if len(str)%4 == 3 {
str += "="
}
en_byt, _ := base64.StdEncoding.DecodeString(str)
return string(en_byt)
}
func Addslashes(str string) string {
var buffer bytes.Buffer
for _, char := range str {
switch char {
case '\'', '"', '\\':
buffer.WriteRune('\\')
}
buffer.WriteRune(char)
}
return buffer.String()
}
func Htmlentities(str string) string {
return html.EscapeString(str)
}
func Html_entity_decode(str string) string {
return html.UnescapeString(str)
}
func Crc32(str string) uint32 {
return crc32.ChecksumIEEE([]byte(str))
}
func Url_encode(str string) string {
return url.QueryEscape(str)
}
func Url_decode(str string) string {
en_str, _ := url.QueryUnescape(str)
return en_str
}
func Http_build_query(queryData url.Values) string {
return queryData.Encode()
}
func Uniqid(prefix ...string) string {
pre := ""
if len(prefix) > 0 {
pre = prefix[0]
}
now := time.Now()
return fmt.Sprintf("%s%08x%05x", pre, now.Unix(), now.UnixNano()%0x100000)
}
func File_exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}
func Is_file(path string) bool {
fi, e := os.Stat(path)
if e != nil {
return false
}
return !fi.IsDir()
}
func Is_dir(path string) bool {
fi, e := os.Stat(path)
if e != nil {
return false
}
return fi.IsDir()
}
func Array_keys(array map[string]interface{}) []string {
var key_array []string
for k, _ := range array {
key_array = append(key_array, k)
}
return key_array
}
func Array_values(array map[string]interface{}) []interface{} {
var value_array []interface{}
for _, v := range array {
value_array = append(value_array, v)
}
return value_array
}
func Array_column(array []map[string]interface{}, column string) []interface{} {
var column_array []interface{}
count := len(array)
for i := 0; i < count; i++ {
if v, ok := array[i][column]; ok {
column_array = append(column_array, v)
} else {
column_array = append(column_array, nil)
}
}
return column_array
}