1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-12 08:14:06 +02:00

Add contains()

This commit is contained in:
Daniel St. Jules
2013-07-22 00:52:45 -04:00
parent feb4c5a36d
commit 8ddb4b91b5
3 changed files with 59 additions and 2 deletions

View File

@@ -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)

View File

@@ -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;
}
}
?>

View File

@@ -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;
}
}
?>