diff --git a/README.md b/README.md index c16ab6b..6984fae 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ A PHP library with a variety of string manipulation functions with multibyte sup * [endsWith](#endswith) * [toSpaces](#tospaces) * [toTabs](#totabs) + * [slugify](#slugify) + * [contains](#contains) * [Tests](#tests) * [License](#license) @@ -303,12 +305,22 @@ dashes. The string is also converted to lowercase. S::slugify('Using strings like fòô bàř') // 'using-strings-like-foo-bar' ``` +##### contains + +S::contains(string $haystack, string $needle [, string $encoding ]) + +Returns true if $haystack contains $needle, false otherwise. + +```php +S::contains('Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8') // true +``` + ## TODO -**contains** - **between** +**surround** + **replace** => substr_replace **insert** @@ -337,6 +349,8 @@ S::slugify('Using strings like fòô bàř') // 'using-strings-like-foo-bar' **isJson** +**isMultibyte** + ## Tests [![Build Status](https://travis-ci.org/danielstjules/Stringy.png)](https://travis-ci.org/danielstjules/Stringy) diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 2679346..19fc5a7 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -466,6 +466,23 @@ class Stringy { return self::dasherize(strtolower($str)); } + /** + * Returns true if $haystack contains $needle, false otherwise. + * + * @param string $haystack String being checked + * @param string $needle Substring to look for + * @param string $encoding The character encoding + * @return bool Whether or not $haystack contains $needle + */ + public static function contains($haystack, $needle, $encoding) { + $encoding = $encoding ?: mb_internal_encoding(); + + if (mb_strpos($haystack, $needle, 0, $encoding) !== false) + return true; + + return false; + } + } ?> diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index 86add32..c483f4a 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -506,6 +506,32 @@ class StringyTestCase extends PHPUnit_Framework_TestCase { return $testData; } + /** + * @dataProvider stringsForContains + */ + public function testContains($expected, $haystack, $needle, $encoding = null) { + $result = S::contains($haystack, $needle, $encoding); + $this->assertEquals($expected, $result); + } + + public function stringsForContains() { + $testData = array( + array(true, 'This string contains foo bar', 'foo bar'), + array(true, '12398!@(*%!@# @!%#*&^%', ' @!%#*&^%'), + array(true, 'Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'), + array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å´¥©', 'UTF-8'), + array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å˚ ∆', 'UTF-8'), + array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'øœ¬', 'UTF-8'), + array(false, 'This string contains foo bar', 'Foo bar'), + array(false, 'This string contains foo bar', 'foobar'), + array(false, 'This string contains foo bar', 'foo bar '), + array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', 'UTF-8'), + array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßå˚', 'UTF-8') + ); + + return $testData; + } + } ?>