Skip to content

Commit

Permalink
Add new feature removeEnd to StringTools
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- committed Dec 18, 2018
1 parent cdbb0f8 commit 9fe26b0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [2.3.0] - 2018-12-18
### Added
- New feature `StringTools::removeEnd`

## [2.2.0] - 2018-07-13
### Added
- New `ArrayTools` class helper
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ StringTools::removeStart($str, $toRemove) : string
* `$str` string input
* `$toRemove` string to remove at the start of `$str`

#### ::removeEnd

Removes the end of the string if it matches with the given text to remove.

```php
StringTools::removeEnd($str, $toRemove) : string
```

* `$str` string input
* `$toRemove` string to remove at the end of `$str`

#### ::contains

```php
Expand Down Expand Up @@ -137,4 +148,4 @@ DateTimeComparator::lowest($dateTime1, $dateTime2) : ?\DateTimeInterface
```

* `$dateTime1` The first \DateTimeInterface or null
* `$dateTime2` The second \DateTimeInterface or null
* `$dateTime2` The second \DateTimeInterface or null
7 changes: 7 additions & 0 deletions spec/Nekland/Tools/StringToolsSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ function it_should_uppercase_first_letter_with_ucfirst()
$this::mb_ucfirst('HelloWorld')->shouldReturn('HelloWorld');
$this::mb_ucfirst('🍕isReallyGood')->shouldReturn('🍕isReallyGood');
}

function it_should_remove_given_end_of_string()
{
$this::removeEnd('I\'m some random text with end to remove', 'end to remove')->shouldReturn('I\'m some random text with ');
$this::removeEnd('I like 🍪 so I remove 🍕', ' so I remove 🍕')->shouldReturn('I like 🍪');
$this::removeEnd('I like pizza', ' and cookies')->shouldReturn('I like pizza');
}
}
15 changes: 15 additions & 0 deletions src/StringTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ public static function removeStart($str, $toRemove)
return substr($str, $sizeToRemove, strlen($str) - $sizeToRemove);
}

/**
* @param string $str
* @param string $toRemove
* @return string
*/
public static function removeEnd($str, $toRemove)
{
if (!StringTools::endsWith($str, $toRemove)) {
return $str;
}
$sizeToRemove = strlen($toRemove);

return substr($str, 0, - $sizeToRemove);
}

/**
* @param string $str The string that should contains the needle
* @param string $needle What should be contained
Expand Down

0 comments on commit 9fe26b0

Please sign in to comment.