Skip to content

Commit 72d4124

Browse files
committed
Added trim functions
1 parent be8aed9 commit 72d4124

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/StringFunctions.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,66 @@ function splitWithExpression($haystack, $needle, $limit = 0)
351351

352352
return preg_split($needle, $haystack, $limit);
353353
}
354+
355+
function trim($haystack, $needle)
356+
{
357+
if (isExpression($needle)) {
358+
return trimWithExpression($haystack, $needle);
359+
}
360+
361+
return trimWithString($haystack, $needle);
362+
}
363+
364+
function trimWithString($haystack, $needle)
365+
{
366+
return \trim($haystack, $needle);
367+
}
368+
369+
function trimWithExpression($haystack, $needle)
370+
{
371+
$pattern = slice($needle, 1, length($needle) - 2);
372+
373+
return preg_replace("#^{$pattern}|{$pattern}$#", "", $haystack);
374+
}
375+
376+
function trimLeft($haystack, $needle)
377+
{
378+
if (isExpression($needle)) {
379+
return trimLeftWithExpression($haystack, $needle);
380+
}
381+
382+
return trimLeftWithString($haystack, $needle);
383+
}
384+
385+
function trimLeftWithString($haystack, $needle)
386+
{
387+
return ltrim($haystack, $needle);
388+
}
389+
390+
function trimLeftWithExpression($haystack, $needle)
391+
{
392+
$pattern = slice($needle, 1, length($needle) - 2);
393+
394+
return preg_replace("#^{$pattern}#", "", $haystack);
395+
}
396+
397+
function trimRight($haystack, $needle)
398+
{
399+
if (isExpression($needle)) {
400+
return trimRightWithExpression($haystack, $needle);
401+
}
402+
403+
return trimRightWithString($haystack, $needle);
404+
}
405+
406+
function trimRightWithString($haystack, $needle)
407+
{
408+
return rtrim($haystack, $needle);
409+
}
410+
411+
function trimRightWithExpression($haystack, $needle)
412+
{
413+
$pattern = slice($needle, 1, length($needle) - 2);
414+
415+
return preg_replace("#{$pattern}$#", "", $haystack);
416+
}

tests/StringFunctionTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,28 @@ public function testStartsWith()
7272
$this->assertTrue(StringFunctions\startsWith("abcdef", "~abc~"));
7373
$this->assertFalse(StringFunctions\startsWith("abcdef", "~def~"));
7474
}
75+
76+
public function testTrim()
77+
{
78+
$this->assertEquals("abc", StringFunctions\trim(" abc ", " "));
79+
$this->assertEquals("abc", StringFunctions\trim(".abc.", "."));
80+
$this->assertEquals("abc", StringFunctions\trim(" abc ", "#\\s#"));
81+
$this->assertEquals("abc", StringFunctions\trim(".abc.", "#\\.#"));
82+
}
83+
84+
public function testTrimLeft()
85+
{
86+
$this->assertEquals("abc ", StringFunctions\trimLeft(" abc ", " "));
87+
$this->assertEquals("abc.", StringFunctions\trimLeft(".abc.", "."));
88+
$this->assertEquals("abc ", StringFunctions\trimLeft(" abc ", "#\\s#"));
89+
$this->assertEquals("abc.", StringFunctions\trimLeft(".abc.", "#\\.#"));
90+
}
91+
92+
public function testTrimRight()
93+
{
94+
$this->assertEquals(" abc", StringFunctions\trimRight(" abc ", " "));
95+
$this->assertEquals(".abc", StringFunctions\trimRight(".abc.", "."));
96+
$this->assertEquals(" abc", StringFunctions\trimRight(" abc ", "#\\s#"));
97+
$this->assertEquals(".abc", StringFunctions\trimRight(".abc.", "#\\.#"));
98+
}
7599
}

0 commit comments

Comments
 (0)