chengkun
2025-09-19 d48eff069585e2be1bd02b1299e1fe7581cb6dad
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
<?php
 
namespace AlibabaCloud\Tea;
 
class Helper
{
    /**
     * @param string   $content
     * @param string   $prefix
     * @param string   $end
     * @param string[] $filter
     *
     * @return string|string[]
     */
    public static function findFromString($content, $prefix, $end, $filter = ['"', ' '])
    {
        $len = mb_strlen($prefix);
        $pos = mb_strpos($content, $prefix);
        if (false === $pos) {
            return '';
        }
        $pos_end = mb_strpos($content, $end, $pos);
        $str     = mb_substr($content, $pos + $len, $pos_end - $pos - $len);
 
        return str_replace($filter, '', $str);
    }
 
    /**
     * @param string $str
     *
     * @return bool
     */
    public static function isJson($str)
    {
        json_decode($str);
 
        return \JSON_ERROR_NONE == json_last_error();
    }
 
    /**
     * @param mixed $value
     *
     * @return bool
     */
    public static function isBytes($value)
    {
        if (!\is_array($value)) {
            return false;
        }
        $i = 0;
        foreach ($value as $k => $ord) {
            if ($k !== $i) {
                return false;
            }
            if (!\is_int($ord)) {
                return false;
            }
            if ($ord < 0 || $ord > 255) {
                return false;
            }
            ++$i;
        }
 
        return true;
    }
 
    /**
     * Convert a bytes to string(utf8).
     *
     * @param array $bytes
     *
     * @return string the return string
     */
    public static function toString($bytes)
    {
        $str = '';
        foreach ($bytes as $ch) {
            $str .= \chr($ch);
        }
 
        return $str;
    }
 
    /**
     * @return array
     */
    public static function merge(array $arrays)
    {
        $result = [];
        foreach ($arrays as $array) {
            foreach ($array as $key => $value) {
                if (\is_int($key)) {
                    $result[] = $value;
 
                    continue;
                }
 
                if (isset($result[$key]) && \is_array($result[$key])) {
                    $result[$key] = self::merge(
                        [$result[$key], $value]
                    );
 
                    continue;
                }
 
                $result[$key] = $value;
            }
        }
 
        return $result;
    }
}