|
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
|
|
|
$strlen = strlen($string); // bytes! |
|
38
|
|
|
mb_ereg_search_init($string); |
|
39
|
|
|
|
|
40
|
|
|
$lengths = array(); |
|
41
|
|
|
$position = 0; |
|
42
|
|
|
while (($array = mb_ereg_search_pos($pattern, '')) !== false) { |
|
43
|
|
|
// capture split |
|
44
|
|
|
$lengths[] = array($array[0] - $position, false, null); |
|
45
|
|
|
|
|
46
|
|
|
// move position |
|
47
|
|
|
$position = $array[0] + $array[1]; |
|
48
|
|
|
|
|
49
|
|
|
// capture delimiter |
|
50
|
|
|
$regs = mb_ereg_search_getregs(); |
|
51
|
|
|
$lengths[] = array($array[1], true, isset($regs[1]) && $regs[1]); |
|
52
|
|
|
|
|
53
|
|
|
// Continue on? |
|
54
|
|
|
if ($position >= $strlen) { |
|
55
|
|
|
break; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Add last bit, if not ending with split |
|
60
|
|
|
$lengths[] = array($strlen - $position, false, null); |
|
61
|
|
|
|
|
62
|
|
|
// Substrings |
|
63
|
|
|
$parts = array(); |
|
64
|
|
|
$position = 0; |
|
65
|
|
|
$count = 1; |
|
66
|
|
|
foreach ($lengths as $length) { |
|
67
|
|
|
$split_empty = $length[0] || !$split_no_empty; |
|
68
|
|
|
$is_delimiter = $length[1]; |
|
69
|
|
|
$is_captured = $length[2]; |
|
70
|
|
|
|
|
71
|
|
|
if ($limit > 0 |
|
72
|
|
|
&& !$is_delimiter |
|
73
|
|
|
&& $split_empty |
|
74
|
|
|
&& ++$count > $limit) { |
|
75
|
|
|
if ($length[0] > 0 |
|
76
|
|
|
|| $split_empty) { |
|
77
|
|
|
$parts[] = $offset_capture |
|
78
|
|
|
? array(mb_strcut($string, $position), $position) |
|
79
|
|
|
: mb_strcut($string, $position); |
|
80
|
|
|
} |
|
81
|
|
|
break; |
|
82
|
|
|
} elseif ((!$is_delimiter |
|
|
|
|
|
|
83
|
|
|
|| ($delim_capture |
|
84
|
|
|
&& $is_captured)) |
|
85
|
|
|
&& ($length[0] |
|
86
|
|
|
|| $split_empty)) { |
|
87
|
|
|
$parts[] = $offset_capture |
|
88
|
|
|
? array(mb_strcut($string, $position, $length[0]), $position) |
|
89
|
|
|
: mb_strcut($string, $position, $length[0]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$position += $length[0]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $parts; |
|
96
|
|
|
} |
|
97
|
|
|
} |