-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtraction.php
More file actions
166 lines (144 loc) · 4.91 KB
/
Copy pathExtraction.php
File metadata and controls
166 lines (144 loc) · 4.91 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
<?php
declare(strict_types=1);
namespace Zero\Lib\Support\Concerns\Str;
trait Extraction
{
public static function limit(string $value, int $limit, string $end = '...'): string
{
if (mb_strlen($value) <= $limit) {
return $value;
}
return rtrim(mb_substr($value, 0, $limit)) . $end;
}
public static function words(string $value, int $words, string $end = '...'): string
{
$value = trim($value);
if ($value === '') {
return '';
}
$segments = preg_split('/\s+/u', $value, -1, PREG_SPLIT_NO_EMPTY);
if ($segments === false || count($segments) <= $words) {
return $value;
}
$limited = array_slice($segments, 0, max($words, 0));
return implode(' ', $limited) . $end;
}
public static function limitWords(string $value, int $words, string $end = '...'): string
{
return self::words($value, $words, $end);
}
public static function substr(string $value, int $start, ?int $length = null, ?string $encoding = null): string
{
return $encoding === null
? ($length === null ? mb_substr($value, $start) : mb_substr($value, $start, $length))
: ($length === null ? mb_substr($value, $start, null, $encoding) : mb_substr($value, $start, $length, $encoding));
}
public static function after(string $subject, string $search): string
{
if ($search === '') {
return $subject;
}
$position = mb_strpos($subject, $search);
if ($position === false) {
return $subject;
}
return mb_substr($subject, $position + mb_strlen($search));
}
public static function before(string $subject, string $search): string
{
if ($search === '') {
return $subject;
}
$position = mb_strpos($subject, $search);
if ($position === false) {
return $subject;
}
return mb_substr($subject, 0, $position);
}
public static function between(string $subject, string $from, string $to): string
{
if ($from === '' || $to === '') {
return '';
}
$result = self::after($subject, $from);
$result = self::before($result, $to);
return $result;
}
public static function afterLast(string $subject, string $search): string
{
if ($search === '') {
return $subject;
}
$position = strrpos($subject, $search);
return $position === false ? $subject : substr($subject, $position + strlen($search));
}
public static function beforeLast(string $subject, string $search): string
{
if ($search === '') {
return $subject;
}
$position = strrpos($subject, $search);
return $position === false ? $subject : substr($subject, 0, $position);
}
public static function betweenFirst(string $subject, string $from, string $to): string
{
if ($from === '' || $to === '') {
return $subject;
}
return self::before(self::after($subject, $from), $to);
}
public static function charAt(string $subject, int $index): string|false
{
$length = mb_strlen($subject);
if ($index < 0) {
$index += $length;
}
if ($index < 0 || $index >= $length) {
return false;
}
return mb_substr($subject, $index, 1);
}
public static function take(string $string, int $limit): string
{
if ($limit < 0) {
return mb_substr($string, $limit);
}
return mb_substr($string, 0, $limit);
}
public static function excerpt(string $text, string $phrase = '', array $options = []): ?string
{
$radius = $options['radius'] ?? 100;
$omission = $options['omission'] ?? '...';
if ($phrase === '') {
return self::limit($text, $radius * 2, $omission);
}
$position = mb_stripos($text, $phrase);
if ($position === false) {
return null;
}
$startPos = max($position - $radius, 0);
$endPos = min($position + mb_strlen($phrase) + $radius, mb_strlen($text));
$prefix = $startPos > 0 ? $omission : '';
$suffix = $endPos < mb_strlen($text) ? $omission : '';
return $prefix . trim(mb_substr($text, $startPos, $endPos - $startPos)) . $suffix;
}
public static function match(string $pattern, string $subject): string
{
preg_match($pattern, $subject, $matches);
if ($matches === []) {
return '';
}
return $matches[1] ?? $matches[0];
}
/**
* @return array<int, string>
*/
public static function matchAll(string $pattern, string $subject): array
{
preg_match_all($pattern, $subject, $matches);
if (empty($matches[0])) {
return [];
}
return $matches[1] ?? $matches[0];
}
}