-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.func.php
410 lines (382 loc) · 9.24 KB
/
text.func.php
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
function is_int_ex($var)
{
if(is_numeric($var) && is_int($var + 0)) return true;
return false;
}
function abslength($str)
{
if(empty($str)) return 0;
if(function_exists('mb_strlen'))
{
return mb_strlen($str,'utf-8');
}
else
{
preg_match_all("/./u", $str, $arr);
return count($arr[0]);
}
}
function is_email($email)
{
$pattern = '/^\w+@(\w+\.)+[a-z]{2,4}$/i';
if(preg_match($pattern, $email)){
return true;
}
else {
return false;
}
}
function info2array($infoStr, $semicolon='|', $semicolon2=',', $colon=':')
{
$arr = array();
$listArr = explode($semicolon, $infoStr);
foreach($listArr as $val)
{
if($val != '')
{
$arr[] = info2assoc($val, $semicolon2, $colon);
}
}
return $arr;
}
function info2assoc($infoStr, $semicolon=';', $colon=':')
{
$arr = array();
$fieldArr = explode($semicolon, $infoStr);
foreach($fieldArr as $val)
{
if($val != '')
{
list($k, $v) = explode($colon, $val);
$arr[$k] = $v;
}
}
return $arr;
}
//中文字符串反转
function cn_strrev($str)
{
if (is_string($str))
{
$len = strlen($str);
$newstr = "";
for ($i=$len-1; $i>=0; $i--)
{
if(ord($str{$i})>160)
{
$newstr .= $str{$i-1}.$str{$i};
$i--;
}
else
{
$newstr.=$str{$i};
}
}
return $newstr;
}
else
{
return false;
}
}
//UTF-8 js escape实现
function js_escape($str)
{
preg_match_all("/[\xc2-\xdf][\x80-\xbf]+|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}|[\x01-\x7f]+/e",$str,$r);
$str = $r[0];
$l = count($str);
for($i=0; $i <$l; $i++)
{
$value = ord($str[$i][0]);
if($value < 223) {
$str[$i] = rawurlencode(utf8_decode($str[$i]));
}
else {
$str[$i] = "%u".strtoupper(bin2hex(iconv("UTF-8","UCS-2",$str[$i])));
}
}
return join("",$str);
}
//UTF-8 js unescape实现
function js_unescape($str)
{
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i++)
{
if ($str[$i] == '%' && $str[$i+1] == 'u')
{
$val = hexdec(substr($str, $i+2, 4));
if ($val < 0x7f) $ret .= chr($val);
else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f));
else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f));
$i += 5;
}
else if ($str[$i] == '%')
{
$ret .= urldecode(substr($str, $i, 3));
$i += 2;
}
else $ret .= $str[$i];
}
return $ret;
}
function asc2hex($str)
{
return '\x'.substr(chunk_split(bin2hex($str), 2, '\x'),0,-2);
}
//十六进制 转 ASCII
function hex2asc($str)
{
$str = join('',explode('\x',$str));
$len = strlen($str);
$data = '';
for ($i=0;$i<$len;$i+=2)
{
$data.= chr(hexdec(substr($str,$i,2)));
}
return $data;
}
function xml2array($xmlStr)
{
$xml_parser = xml_parser_create();
if(!xml_parse($xml_parser,$xmlStr,true))
{
xml_parser_free($xml_parser);
return false;
}
else
{
return json_decode(json_encode(simplexml_load_string($xmlStr,'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS)),true);
}
}
function array2xml($array)
{
if(is_array($array))
{
$xml = '';
foreach ($array as $key=>$val)
{
if (is_array($val)) {
$xml .= "<$key>".array2xml($val)."</$key>";
}
else {
$xml .= "<$key>".$val."</$key>";
}
}
return $xml;
}
else
{
return '';
}
}
function cutstr($str,$cutleng){
mb_internal_encoding("UTF-8");
return mb_substr($str,0,$cutleng);
}
function cn_substr($str, $start=0, $length, $charset="utf-8", $suffix=false)
{
if(function_exists("mb_substr"))
{
if(mb_strlen($str, $charset) <= $length) return $str;
$slice = mb_substr($str, $start, $length, $charset);
}
else
{
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
if(count($match[0]) <= $length) return $str;
$slice = join("",array_slice($match[0], $start, $length));
}
if($suffix) return $slice."…";
return $slice;
}
function info2mix($str)
{
$list = array();
$arr = explode(';', $str);
$pattern = '/(\w+):\[(.*?)\]/';
foreach($arr as $val)
{
if($val != '')
{
$attrStr = preg_replace($pattern, '',$val);
$t = info2assoc($attrStr, ',', ':');
preg_match_all($pattern, $val, $match, PREG_SET_ORDER);
foreach($match as $k=>$v)
{
$t[$v[1]] = info2array($v[2],'|', ',', ':');
}
$list[] = $t;
}
}
return $list;
}
// 闭合标签
function closetags($html)
{
preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
$openedtags = $result[1];
preg_match_all('#</([a-z]+)>#iU', $html, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
if (count($closedtags) == $len_opened) {
return $html;
}
$openedtags = array_reverse($openedtags);
for ($i=0; $i < $len_opened; $i++) {
if (!in_array($openedtags[$i], $closedtags)){
$html .= '</'.$openedtags[$i].'>';
} else {
unset($closedtags[array_search($openedtags[$i], $closedtags)]);
}
}
return $html;
}
function filter_style($content, $div=true)
{
if($div) $content = preg_replace('/<div[^>]*>|<\/div>/i','',$content);//去<div>
$content = preg_replace("/style=.+?['|\"]/i",'',$content);//去除样式
$content = preg_replace("/class=.+?['|\"]/i",'',$content);//去除样式
$content = preg_replace("/id=.+?['|\"]/i",'',$content);//去除样式
$content = preg_replace("/width=.+?['|\"]/i",'',$content);//去除样式
$content = preg_replace("/height=.+?['|\"]/i",'',$content);//去除样式
$content = preg_replace("/border=.+?['|\"]/i",'',$content);//去除样式
return $content;
}
/*
id,article_id,position,title,url,cover,desc
(空行,不参与解析)
# 热门推荐 (#开头为注释,不解析)
1001,null,hot,hot_1,http://www.wanmei.com,null,null
1002,null,hot,hot_2,http://www.wanmei.com,null,null
1003,null,hot,hot_3,http://www.wanmei.com,null,null
1004,null,hot,hot_4,http://www.wanmei.com,null,null
1005,null,hot,hot_5,http://www.wanmei.com,null,null
1006,null,hot,hot_6,http://www.wanmei.com,/uploads/1510/26/4009-15102611560b34.jpg,'摘要'
*/
function listtext2array($str)
{
$array = $headers = array();
$rows = explode("\n", str_replace("\r" , "", $str));
if(isset($rows[0]) && substr($rows[0], 0, 2) == '#,')
{
$headers = explode(',', $rows[0]);
}
foreach($rows as $key=>$row)
{
if($key == 0) continue;
$trim = trim($row);
if(empty($trim)) continue;
if(substr($trim, 0, 1) == '#') continue;
$t = array();
$row = explode(',', $row);
foreach($headers as $k=>$header)
{
$t[$header] = isset($row[$k])? trim($row[$k]) : '';
}
$array[] = $t;
}
return $array;
}
//短地址
function short_encode($url)
{
$key = 'sh';
$chars = array('a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t','u','v','w',
'x','y','z','0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$short = array();
$hex = md5($url.$key);
for($i=0; $i<4; $i++)
{
$sub_hex = substr($hex, $i*8, 8);
//3FFFFFFF = 30位1 = 6*5
$int = 0x3FFFFFFF & ('0x'.$sub_hex-0);//字符串转换成float型的10进制
$sub_short = '';
for($j=0; $j<6; $j++)
{
$sub_short .= $chars[0x0000003D & $int];
$int = $int >> 5;
}
exit;
$short[] = $sub_short;
}
return $short;
}
function short_encode2($in)
{
$chars = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$hex8 = substr(md5($in), 0, 8);
$hex_int = base_convert($hex8, 16, 10);
$hex = 0x3FFFFFFF & $hex_int;
$out = '';
for($i=6; $i>0; --$i)
{
$index = 0x0000003D & $hex;
$out .= $chars[$index];
$hex = $hex >> 5;
}
return $out;
}
/**
* 由于php的json扩展自带的函数json_encode会将汉字转换成unicode码
* 所以我们在这里用自定义的json_encode,这个函数不会将汉字转换为unicode码
*/
function json_encode_ex($a=false)
{
if (is_null($a))
return 'null';
if ($a === false)
return 'false';
if ($a === true)
return 'true';
if (is_scalar($a))
{ //判断是否为一个标量
if (is_float($a))
return floatval(str_replace(",",".",strval($a)));//将变量转换为字段串类型
if (is_string($a))
{
static $jsonReplaces =array(array("\\","/","\n","\t","\r","\b","\f",'"'),array('\\\\','\\/','\\n','\\t','\\r','\\b','\\f','\"'));
return '"' .str_replace($jsonReplaces[0],$jsonReplaces[1],$a) . '"';
}
else
{
return $a;
}
}
$isList = true; //判断键值是否为自增长,也就是键值是从0开始自动添加的,不是自定义的
for ($i = 0, reset($a);$i <count($a);$i++, next($a))
{
if (key($a) !== $i)
{
$isList = false;
break;
}
}
$result =array();
if ($isList)
{
foreach ($a as $v)
{
$result[] = json_encode_ex($v);
}
return '[' . join(',',$result) . ']';
}
else
{
foreach ($a as $k =>$v)
{
$result[] = json_encode_ex($k) . ':' .json_encode_ex($v);
}
return '{' . join(',',$result) . '}';
}
}