Skip to content

Commit 1022bbc

Browse files
committed
Add preg split and trim helper
1 parent 2cc2256 commit 1022bbc

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

bootstrap/helpers.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ function str_chop(string $value, int $limit, string $center = '...'): string
2121
}
2222
}
2323

24+
if (! function_exists('preg_split_trim')) {
25+
function preg_split_trim(string $value, string $pattern = '/,|;|\s+/', int $sort = -1): array
26+
{
27+
$items = array_map('trim', preg_split($pattern, $value));
28+
$items = array_filter($items);
29+
30+
if ($sort >= SORT_REGULAR) {
31+
sort($items, $sort);
32+
}
33+
34+
return array_values($items);
35+
}
36+
}
37+
2438
if (! function_exists('crush_png')) {
2539
function crush_png(string $image): bool
2640
{

tests/Feature/HelpersTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
it('has the callable helpers', function () {
44
expect('encode_json')->toBeCallable()
5+
->and('preg_split_trim')->toBeCallable()
56
->and('decode_json')->toBeCallable()
67
->and('crush_png')->toBeCallable()
78
->and('str_chop')->toBeCallable();
@@ -40,6 +41,17 @@
4041

4142
})->covers('str_chop');
4243

44+
it('splits and trims a string', function () {
45+
$value = 'one, two; three,;
46+
four;five,,';
47+
48+
expect(preg_split_trim($value))
49+
->toBeArray()
50+
->toContain('three')
51+
->toHaveLength(5);
52+
53+
})->covers('preg_split_trim');
54+
4355
it('has pngcrush installed', function () {
4456
exec(' LC_ALL=C type pngcrush', $output, $result);
4557
expect($output)

0 commit comments

Comments
 (0)