Passed
Push — v6 ( aac15a...ba732b )
by 光春
04:36
created

Strings::filter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkLibrary 6.0 for ThinkPhP 6.0
5
// +----------------------------------------------------------------------
6
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
7
// +----------------------------------------------------------------------
8
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
9
// +----------------------------------------------------------------------
10
// | 开源协议 ( https://mit-license.org )
11
// +----------------------------------------------------------------------
12
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
13
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
14
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
15
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
16
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
17
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
18
// +----------------------------------------------------------------------
19
20
declare (strict_types=1);
21
22
namespace DtApp\ThinkLibrary\helper;
23
24
/**
25
 * 字符串管理类
26
 * @mixin Strings
27
 * @package DtApp\ThinkLibrary\helper
28
 */
29
class Strings
30
{
31
    /**
32
     * 截取字符串前面n个字符
33
     * @param string $str 字符串
34
     * @param int $start_num 开始位置
35
     * @param int $end_num 多少个
36
     * @return string
37
     */
38
    public function extractBefore(string $str, int $start_num, int $end_num): string
39
    {
40
        if (strlen($str) < $start_num + $end_num) {
41
            return $str;
42
        }
43
        return substr($str, $start_num, $end_num);
44
    }
45
46
    /**
47
     * 截取字符串最后n个字符
48
     * 截取字符串最后n个字符
49
     * @param string $str 字符串
50
     * @param int $num 多少个
51
     * @return string
52
     */
53
    public function extractRear(string $str, int $num): string
54
    {
55
        if (strlen($str) <= $num) {
56
            return $str;
57
        }
58
        return substr($str, -$num);
59
    }
60
61
    /**
62
     * 过滤字符串
63
     * @param string $str
64
     * @return string
65
     */
66
    public function filter(string $str): string
67
    {
68
        $str = str_replace(array('`', '·', '~', '!', '!', '@', '#', '$', '¥', '%', '^', '……', '&', '*', '(', ')', '(', ')', '-', '_', '——', '+', '=', '|', '\\', '[', ']', '【', '】', '{', '}', ';', ';', ':', ':', '\'', '"', '“', '”', ',', ',', '<', '>', '《', '》', '.', '。', '/', '、', '?', '?', '╮', '(', ')', 'r', 'ぷ', '〆', 'ゞ', 'ヤ', 'ゼ', 'ǎ', 'ǎ', '〆', 'む', '§', '上门'), '', $str);
69
        return trim($str);
70
    }
71
72
    /**
73
     * 判断字符串是否包含某个字符
74
     * @param $str
75
     * @param int $nee
76
     * @param string $del
77
     * @return bool
78
     */
79
    public function exitContain(string $str, $nee = 3, $del = ','): bool
80
    {
81
        if (strpos($str, $del) !== false) {
82
            $var = explode($del, $str);
83
            foreach ($var as $v) {
84
                if ($v == $nee) {
85
                    return true;
86
                }
87
            }
88
            return false;
89
        }
90
91
        return $str == $nee;
92
    }
93
94
    /**
95
     * 统计字符串长度
96
     * @param string $str 字符串
97
     * @return int
98
     */
99
    public function len(string $str): int
100
    {
101
        return strlen($str);
102
    }
103
104
    /**
105
     * 字符串删除空格
106
     * @param $str
107
     * @return string|string[]
108
     */
109
    public function trimAll($str): string
110
    {
111
        $oldchar = array(" ", " ", "\t", "\n", "\r");
112
        $newchar = array("", "", "", "", "");
113
        return str_replace($oldchar, $newchar, $str);
114
    }
115
116
    /**
117
     * 替换字符串
118
     * @param string $search
119
     * @param string $replace
120
     * @param string $subject
121
     * @return string|string[]
122
     */
123
    public function replace(string $search, string $replace, string $subject)
124
    {
125
        return str_replace($search, $replace, $subject);
126
    }
127
}
128