Passed
Push — master ( 1e5204...e85d9b )
by Martijn
01:36
created

Multibyte::trim()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Vanderlee\Sentence;
4
5
/**
6
 * Multibyte-safe utility functions
7
 */
8
class Multibyte
9
{
10
    /**
11
     * Multibyte.php safe version of standard trim() function.
12
     *
13
     * @param string $string
14
     * @return string
15
     */
16
    public static function trim($string)
17
    {
18
        return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string);
19
    }
20
21
    /**
22
     * A cross between mb_split and preg_split, adding the preg_split flags
23
     * to mb_split.
24
     *
25
     * @param string $pattern
26
     * @param string $string
27
     * @param int $limit
28
     * @param int $flags
29
     * @return array
30
     */
31
    public static function split($pattern, $string, $limit = -1, $flags = 0)
32
    {
33
        $split_no_empty = (bool)($flags & PREG_SPLIT_NO_EMPTY);
34
        $offset_capture = (bool)($flags & PREG_SPLIT_OFFSET_CAPTURE);
35
        $delim_capture = (bool)($flags & PREG_SPLIT_DELIM_CAPTURE);
36
37
        $lengths = self::getSplitLengths($pattern, $string);
38
39
        // Substrings
40
        $parts = [];
41
        $position = 0;
42
        $count = 1;
43
        foreach ($lengths as $length) {
44
            $split_empty = !$split_no_empty || $length[0];
45
            $is_delimiter = $length[1];
46
            $is_captured = $delim_capture && $length[2];
47
48
            if ($limit > 0
49
                && !$is_delimiter
50
                && $split_empty
51
                && ++$count > $limit) {
52
53
                $cut = mb_strcut($string, $position);
54
55
                $parts[] = $offset_capture
56
                    ? [$cut, $position]
57
                    : $cut;
58
59
                break;
60
            } elseif ((!$is_delimiter
61
                    || $is_captured)
62
                && $split_empty) {
63
64
                $cut = mb_strcut($string, $position, $length[0]);
65
66
                $parts[] = $offset_capture
67
                    ? [$cut, $position]
68
                    : $cut;
69
            }
70
71
            $position += $length[0];
72
        }
73
74
        return $parts;
75
    }
76
77
    /**
78
     * Splits the string by pattern and for each element (part or split) returns:
79
     *  [ 0 => length, 1 => is_delimiter?, 2 =>
80
     *
81
     * @param $pattern
82
     * @param $string
83
     * @return array
84
     */
85
    private static function getSplitLengths($pattern, $string)
86
    {
87
        $strlen = strlen($string); // bytes!
88
        $lengths = [];
89
90
        mb_ereg_search_init($string);
91
92
        $position = 0;
93
        while ($position < $strlen
94
            && ($array = mb_ereg_search_pos($pattern, '')) !== false) {
95
            // capture split
96
            $lengths[] = [$array[0] - $position, false, null];
97
98
            // move position
99
            $position = $array[0] + $array[1];
100
101
            // capture delimiter
102
            $regs = mb_ereg_search_getregs();
103
            $lengths[] = [$array[1], true, isset($regs[1]) && $regs[1]];
104
        }
105
106
        // Add last bit, if not ending with split
107
        $lengths[] = [$strlen - $position, false, null];
108
109
        return $lengths;
110
    }
111
}