diff --git a/README.md b/README.md index 3d92e76..d37c114 100644 --- a/README.md +++ b/README.md @@ -44,8 +44,12 @@ Note: The methods listed below are subject to change until we reach a 1.0.0 rele * [at](#at) * [first](#first) * [last](#last) - * [ensureLeft](#ensureLeft) - * [ensureRight](#ensureRight) + * [ensureLeft](#ensureleft) + * [ensureRight](#ensureright) + * [chompLeft](#chompleft) + * [chompRight](#chompright) + * [removeLeft](#removeleft) + * [removeRight](#removeright) * [Tests](#tests) * [License](#license) @@ -674,6 +678,32 @@ S::create('foobar')->ensureRight('.com'); S::ensureRight('foobar', '.com'); // 'foobar.com' ``` +##### removeLeft + +$stringy->removeLeft(string $substring) + +S::removeLeft(string $str, string $substring [, string $encoding ]) + +Removes the prefix $substring if present. + +```php +S::create('fòô bàř', 'UTF-8')->removeLeft('fòô '); +S::removeLeft('fòô bàř', 'fòô ', 'UTF-8'); // 'bàř' +``` + +##### removeRight + +$stringy->removeRight(string $substring) + +S::removeRight(string $str, string $substring [, string $encoding ]) + +Removes the suffix $substring if present. + +```php +S::create('fòô bàř', 'UTF-8')->removeRight(' bàř'); +S::removeRight('fòô bàř', ' bàř', 'UTF-8'); // 'fòô' +``` + ## TODO **count** => substr_count @@ -698,10 +728,6 @@ S::ensureRight('foobar', '.com'); // 'foobar.com' **isBlank** -**chompLeft** - -**chompRight** - ## Tests [![Build Status](https://travis-ci.org/danielstjules/Stringy.png)](https://travis-ci.org/danielstjules/Stringy) diff --git a/src/Stringy/StaticStringy.php b/src/Stringy/StaticStringy.php index 9843d65..d47f5b4 100644 --- a/src/Stringy/StaticStringy.php +++ b/src/Stringy/StaticStringy.php @@ -550,4 +550,30 @@ class StaticStringy { return Stringy::create($str, $encoding)->ensureRight($substring)->str; } + + /** + * Removes the prefix $substring if present. + * + * @param string $str String from which to remove the prefix + * @param string $substring The prefix to remove + * @param string $encoding The character encoding + * @return string The string without the prefix $substring + */ + public static function removeLeft($str, $substring, $encoding = null) + { + return Stringy::create($str, $encoding)->removeLeft($substring)->str; + } + + /** + * Removes the suffix $substring if present. + * + * @param string $str String from which to remove the suffix + * @param string $substring The suffix to remove + * @param string $encoding The character encoding + * @return string The string without the suffix $substring + */ + public static function removeRight($str, $substring, $encoding = null) + { + return Stringy::create($str, $encoding)->removeRight($substring)->str; + } } diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index e568c64..b4c6e34 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -852,4 +852,36 @@ class Stringy return $this; } + + /** + * Removes the prefix $substring if present. + * + * @param string $substring The prefix to remove + * @return Stringy Object having a $str without the prefix $substring + */ + public function removeLeft($substring) + { + if ($this->startsWith($substring)) { + $substringLength = mb_strlen($substring, $this->encoding); + $this->substr($substringLength); + } + + return $this; + } + + /** + * Removes the suffix $substring if present. + * + * @param string $substring The suffix to remove + * @return Stringy Object having a $str without the suffix $substring + */ + public function removeRight($substring) + { + if ($this->endsWith($substring)) { + $substringLength = mb_strlen($substring, $this->encoding); + $this->substr(0, $this->length() - $substringLength); + } + + return $this; + } } diff --git a/tests/Stringy/CommonTest.php b/tests/Stringy/CommonTest.php index cfdb5b9..d3d36c7 100644 --- a/tests/Stringy/CommonTest.php +++ b/tests/Stringy/CommonTest.php @@ -669,6 +669,42 @@ class CommonTest extends PHPUnit_Framework_TestCase return $testData; } + public function stringsForRemoveLeft() + { + $testData = array( + array('foo bar', 'foo bar', ''), + array('oo bar', 'foo bar', 'f'), + array('bar', 'foo bar', 'foo '), + array('foo bar', 'foo bar', 'oo'), + array('foo bar', 'foo bar', 'oo bar'), + array('fòô bàř', 'fòô bàř', '', 'UTF-8'), + array('òô bàř', 'fòô bàř', 'f', 'UTF-8'), + array('bàř', 'fòô bàř', 'fòô ', 'UTF-8'), + array('fòô bàř', 'fòô bàř', 'òô', 'UTF-8'), + array('fòô bàř', 'fòô bàř', 'òô bàř', 'UTF-8') + ); + + return $testData; + } + + public function stringsForRemoveRight() + { + $testData = array( + array('foo bar', 'foo bar', ''), + array('foo ba', 'foo bar', 'r'), + array('foo', 'foo bar', ' bar'), + array('foo bar', 'foo bar', 'ba'), + array('foo bar', 'foo bar', 'foo ba'), + array('fòô bàř', 'fòô bàř', '', 'UTF-8'), + array('fòô bà', 'fòô bàř', 'ř', 'UTF-8'), + array('fòô', 'fòô bàř', ' bàř', 'UTF-8'), + array('fòô bàř', 'fòô bàř', 'bà', 'UTF-8'), + array('fòô bàř', 'fòô bàř', 'fòô bà', 'UTF-8') + ); + + return $testData; + } + // A test is required so as not to throw an error // This is a lot cleaner than using PHPUnit's mocks to spy public function test() { diff --git a/tests/Stringy/StaticStringyTest.php b/tests/Stringy/StaticStringyTest.php index 1eaa0c5..0113a48 100644 --- a/tests/Stringy/StaticStringyTest.php +++ b/tests/Stringy/StaticStringyTest.php @@ -372,4 +372,22 @@ class StaticStringyTestCase extends CommonTest $result = S::ensureRight($str, $substring, $encoding); $this->assertEquals($expected, $result); } + + /** + * @dataProvider stringsForRemoveLeft + */ + public function testRemoveLeft($expected, $str, $substring, $encoding = null) + { + $result = S::removeLeft($str, $substring, $encoding); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForRemoveRight + */ + public function testRemoveRight($expected, $str, $substring, $encoding = null) + { + $result = S::removeRight($str, $substring, $encoding); + $this->assertEquals($expected, $result); + } } diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index 5f52d56..ea6ff1d 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -354,4 +354,22 @@ class StringyTestCase extends CommonTest $result = S::create($str, $encoding)->ensureRight($substring); $this->assertEquals($expected, $result); } + + /** + * @dataProvider stringsForRemoveLeft + */ + public function testRemoveLeft($expected, $str, $substring, $encoding = null) + { + $result = S::create($str, $encoding)->removeLeft($substring); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForRemoveRight + */ + public function testRemoveRight($expected, $str, $substring, $encoding = null) + { + $result = S::create($str, $encoding)->removeRight($substring); + $this->assertEquals($expected, $result); + } }