diff --git a/README.md b/README.md index 6984fae..118df37 100644 --- a/README.md +++ b/README.md @@ -315,16 +315,28 @@ Returns true if $haystack contains $needle, false otherwise. S::contains('Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8') // true ``` +##### surround + +S::surround(string $str, string $substring) + +Surrounds a string with the given substring. + +```php +S::surround(' ͜ ', 'ʘ'); // 'ʘ ͜ ʘ' +``` + +##### insert + +S::insert(string $str, int $index, string $substring [, string $encoding ]) + +Inserts $substring into $str at the $index provided. + +```php +S::insert('fòô bà', 'ř', 6, 'UTF-8'); // 'fòô bàř' +``` + ## TODO -**between** - -**surround** - -**replace** => substr_replace - -**insert** - **truncate** **prune** diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 19fc5a7..589f607 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -474,7 +474,7 @@ class Stringy { * @param string $encoding The character encoding * @return bool Whether or not $haystack contains $needle */ - public static function contains($haystack, $needle, $encoding) { + public static function contains($haystack, $needle, $encoding = null) { $encoding = $encoding ?: mb_internal_encoding(); if (mb_strpos($haystack, $needle, 0, $encoding) !== false) @@ -483,6 +483,38 @@ class Stringy { return false; } + /** + * Surrounds a string with the given substring. + * + * @param string $str The string to surround + * @param string $substring The substring to add to both sides + * @return string The string with the substring prepended and appended + */ + public static function surround($str, $substring) { + return implode('', array($substring, $str, $substring)); + } + + /** + * Inserts $substring into $str at the $index provided. + * + * @param string $str String to insert into + * @param string $substring String to be inserted + * @param int $index The index at which to insert the substring + * @param string $encoding The character encoding + * @return string The resulting string after the insertion + */ + public static function insert($str, $substring, $index, $encoding = null) { + $encoding = $encoding ?: mb_internal_encoding(); + + if ($index > mb_strlen($str)) + return $str; + + $start = mb_substr($str, 0, $index, $encoding); + $end = mb_substr($str, $index, mb_strlen($str), $encoding); + + return $start . $substring . $end; + } + } ?> diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index c483f4a..2f72e76 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -532,6 +532,49 @@ class StringyTestCase extends PHPUnit_Framework_TestCase { return $testData; } + /** + * @dataProvider stringsForSurround + */ + public function testSurround($expected, $string, $substring) { + $result = S::surround($string, $substring); + $this->assertEquals($expected, $result); + } + + public function stringsForSurround() { + $testData = array( + array('__foobar__', 'foobar', '__'), + array('test', 'test', ''), + array('**', '', '*'), + array('¬fòô bàř¬', 'fòô bàř', '¬'), + array('ßå∆˚ test ßå∆˚', ' test ', 'ßå∆˚') + ); + + return $testData; + } + + /** + * @dataProvider stringsForInsert + */ + public function testInsert($expected, $string, $substring, $index, + $encoding = null) { + $result = S::insert($string, $substring, $index, $encoding); + $this->assertEquals($expected, $result); + } + + public function stringsForInsert() { + $testData = array( + array('foo bar', 'oo bar', 'f', 0), + array('foo bar', 'f bar', 'oo', 1), + array('f bar', 'f bar', 'oo', 20), + array('foo bar', 'foo ba', 'r', 6), + array('fòô bàř', 'òô bàř', 'f', 0, 'UTF-8'), + array('fòô bàř', 'f bàř', 'òô', 1, 'UTF-8'), + array('fòô bàř', 'fòô bà', 'ř', 6, 'UTF-8') + ); + + return $testData; + } + } ?>