Strings.php
12.2 KB
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
<?php
namespace tinymeng\tools;
/**
* Class 字符串类
* @package tinymeng\tools
* @Author: TinyMeng <666@majiameng.com>
* @Created: 2018/11/26
*/
class Strings
{
/**
* Function Name: 手写字母大写
* @param $str
* @return string
* @author Tinymeng <666@majiameng.com>
* @date: 2019/9/26 10:19
*/
static public function uFirst($str):string{
return ucfirst(strtolower($str));
}
/**
* Name: 生成随机字符串
* Author: Tinymeng <666@majiameng.com>
* @param int $length 字符串长度
* @return string
*/
public static function generateRandomString($length = 10):string {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
return substr(str_shuffle($characters), 0, $length);
}
/**
* 获取唯一设备号
* @Author: TinyMeng <666@majiameng.com>
* @param string $namespace
* @return string
*/
static public function createChannelId($namespace = ''):string {
static $guid = '';
$uid = uniqid("", true);
$data = $namespace. md5(time() . mt_rand(1,1000000)).uniqid();
$hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
$guid = substr($hash, 0, 8) .
'-' .
substr($hash, 8, 4) .
'-' .
substr($hash, 12, 4) .
'-' .
substr($hash, 16, 4) .
'-' .
substr($hash, 20, 12);
return $guid;
}
/**
* 获取md5中16位小写
* 实现java的 MD516.Bit16
* @Author: TinyMeng <666@majiameng.com>
* @param $str
* @return string
*/
static public function md5Bit16($str):string {
return strtoupper(substr(md5($str),8,16));
}
/**
* 获取时间戳(13位精确到豪妙)
* @Author: TinyMeng <666@majiameng.com>
* @param null $time
* @return int
*/
static public function millisecond($time = null) :int{
if(empty($time)){
list($msec, $sec) = explode(' ', microtime());
$millisecond = (int)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
}else{
$millisecond = strtotime($time)."000";
}
return (int)$millisecond;
}
/**
* Name: 是否包含中文
* Author: Tinymeng <666@majiameng.com>
* @param string $string
* @return bool
*/
public static function isContainChinese($string=''):bool {
$result = preg_match('/[\x{4e00}-\x{9fa5}]/u', $string);
return $result == 0 ? false : true;
}
/**
* Name: 是否全是中文
* Author: Tinymeng <666@majiameng.com>
* @param string $string
* @return bool
*/
public static function isAllChinese($string=''):bool {
$result = preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $string);
return $result == 0 ? false : true;
}
/**
* 判断手机号码
* Author : MYL <ixiaomu@qq.com>
* Updater:
* @param string $string
* @return bool
*/
public static function isMobile($string=''):bool {
if (!preg_match("/(^1[3|4|5|7|8][0-9]{9}$)/", $string)) {
return false;
}
return true;
}
/**
* Description: 科学计数法转化正常数值输出
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @param string $num 科学计数法字符串 如 2.1E-5
* @param int $double 小数点保留位数 默认3位
* @return string
*/
public static function sctonum($num, $double = 3){
if(false !== stripos($num, "e")){
$a = explode("e",strtolower($num));
return bcmul($a[0], bcpow(10, $a[1], $double), $double);
}
return $num;
}
/**
* Name: 自动转换字符集 支持数组转换
* Author: Tinymeng <666@majiameng.com>
* @param $string
* @param string $from
* @param string $to
* @return array|false|string|string[]|null
*/
public static function autoCharset($string, $from='gbk', $to='utf-8') {
$from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
$to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
if (strtoupper($from) === strtoupper($to) || empty($string) || (is_scalar($string) && !is_string($string))) {
//如果编码相同或者非字符串标量则不转换
return $string;
}
if (is_string($string)) {
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($string, $to, $from);
} elseif (function_exists('iconv')) {
return iconv($from, $to, $string);
} else {
return $string;
}
} elseif (is_array($string)) {
foreach ($string as $key => $val) {
$_key = self::autoCharset($key, $from, $to);
$string[$_key] = self::autoCharset($val, $from, $to);
if ($key != $_key)
unset($string[$key]);
}
return $string;
}
else {
return $string;
}
}
/**
* Description: 过滤html里a标签
* Author: song <53804059@qq.com>
* Updater:
* @param $html
* @return string
*/
public static function filterATag($html=''):string {
return preg_replace("#<a[^>]*>(.*?)</a>#is", "$1", $html);
}
/**
* Description: 删除html里a标签及内容
* Author: song <53804059@qq.com>
* Updater:
* @param $html
* @return string
*/
public static function deleteATag($html=''):string {
return preg_replace("#<a[^>]*>(.*?)</a>#is", "", $html);
}
/**
* Description: 时间转换
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @param string $date 时间
* @param bool $is_timestamp 是否是时间戳
* @return string
*/
public static function getTime($date,$is_timestamp=false):string {
if($is_timestamp == true){
$time = $date;
}else{
$time = strtotime($date);//时间转换为时间戳
}
if($time >= time()){
return '刚刚';
}
$seconds = time() - $time;
if($seconds <= 60){
return '刚刚';
}
$minutes = intval($seconds / 60);
if($minutes <= 60){
return $minutes.'分钟前';
}
$hours = intval($minutes / 60);
if($hours <= 24){
return $hours.'小时前';
}
$days = intval($hours / 24);
if($days <= 3){
return $days.'天前';
}
if($days <= 365){
return date('m-d',$time);
}
return date('Y-m-d',$time);
}
/**
* Name: 压缩html代码
* Author: Tinymeng <666@majiameng.com>
* @param string $html_source
* @return string
*/
static public function compressHtml($html_source):string {
$chunks = preg_split('/(<!--<nocompress>-->.*?<!--<\/nocompress>-->|<nocompress>.*?<\/nocompress>|<pre.*?\/pre>|<textarea.*?\/textarea>|<script.*?\/script>)/msi', $html_source, -1, PREG_SPLIT_DELIM_CAPTURE);
$compress = '';
foreach ($chunks as $c) {
if (strtolower(substr($c, 0, 19)) == '<!--<nocompress>-->') {
$c = substr($c, 19, strlen($c) - 19 - 20);
$compress .= $c;
continue;
} elseif (strtolower(substr($c, 0, 12)) == '<nocompress>') {
$c = substr($c, 12, strlen($c) - 12 - 13);
$compress .= $c;
continue;
} elseif (strtolower(substr($c, 0, 4)) == '<pre' || strtolower(substr($c, 0, 9)) == '<textarea') {
$compress .= $c;
continue;
} elseif (strtolower(substr($c, 0, 7)) == '<script' && strpos($c, '//') != false && (strpos($c, "\r") !== false || strpos($c, "\n") !== false)) { // JS代码,包含“//”注释的,单行代码不处理
$tmps = preg_split('/(\r|\n)/ms', $c, -1, PREG_SPLIT_NO_EMPTY);
$c = '';
foreach ($tmps as $tmp) {
if (strpos($tmp, '//') !== false) { // 对含有“//”的行做处理
if (substr(trim($tmp), 0, 2) == '//') { // 开头是“//”的就是注释
continue;
}
$chars = preg_split('//', $tmp, -1, PREG_SPLIT_NO_EMPTY);
$is_quot = $is_apos = false;
foreach ($chars as $key => $char) {
if ($char == '"' && !$is_apos && $key > 0 && $chars[$key - 1] != '\\') {
$is_quot = !$is_quot;
} elseif ($char == '\'' && !$is_quot && $key > 0 && $chars[$key - 1] != '\\') {
$is_apos = !$is_apos;
} elseif ($char == '/' && $chars[$key + 1] == '/' && !$is_quot && !$is_apos) {
$tmp = substr($tmp, 0, $key); // 不是字符串内的就是注释
break;
}
}
}
$c .= $tmp;
}
}
$c = preg_replace('/[\\n\\r\\t]+/', ' ', $c); // 清除换行符,清除制表符
$c = preg_replace('/\\s{2,}/', ' ', $c); // 清除额外的空格
$c = preg_replace('/>\\s</', '> <', $c); // 清除标签间的空格
$c = preg_replace('/\\/\\*.*?\\*\\//i', '', $c); // 清除 CSS & JS 的注释
$c = preg_replace('/<!--[^!]*-->/', '', $c); // 清除 HTML 的注释
$compress .= $c;
}
return $compress;
}
/**
* Description: html标签替换成特定小程序标签
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @param string $content
* @return mixed
*/
static public function htmlReplaceXcx(string $content):string {
$content = str_replace("\r\n","",$content);//出除回车和换行符
$content = preg_replace("/style=\".*?\"/si",'',$content);//style样式
$content = preg_replace(["/<strong.*?>/si", "/<\/strong>/si"],['<text class="wx-strong">','</text>'],$content);//strong
$content = preg_replace(["/<p.*?>/si", "/<\/p>/si"],['<view class="wx-p">','</view>'],$content);//p
$content = preg_replace(["/<a.*?>/si", "/<\/a>/si"],['<text class="wx-a">','</text>'],$content);//a
$content = preg_replace(["/<span.*?>/si", "/<\/span>/si"],['<text class="wx-span">','</text>'],$content);//span
$content = preg_replace(["/<h[1-6].*?>/si", "/<\/h[1-6]>/si"],['<view class="wx-h">','</view>'],$content);//h
$content = preg_replace("/<img.*?/si",'<image class="wx-img"',$content);//img
return $content;
}
/**
* Description: html P标签替换成特定Span标签(安卓app使用)
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @param string $content
* @return string
*/
static public function pReplaceSpan(string $content):string {
$content = str_replace(["\r","\n","\t"],'',$content);
$content = preg_replace(["/<p/si", "/<\/p>/si"],['<span','</span><br>'],$content);//p
return $content;
}
/**
* Description: 过滤标点符号
* @author: JiaMeng <666@majiameng.com>
* Updater:
* @param string $keyword
* @return string
*/
static public function filterPunctuation($keyword){
$keyword = str_replace(["\r\n", "\r", "\n"," "," "], "", trim($keyword));//删除空格
$keyword = preg_replace('# #','',$keyword);
$keyword = preg_replace("/[ '.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",'',$keyword);
return $keyword;
}
/**
* Description: 过滤html标签
* @author: JiaMeng <666@majiameng.com>
* Updater:
* @param $content
* @param string $allowable_tags
* @return string
*/
static public function stripTags($content,$allowable_tags = '<font>'){
$content = strip_tags($content,$allowable_tags);//替换标签
$content = str_replace(["\r\n", "\r", "\n"," "], "", trim($content));//删除空格
return $content;
}
}