mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-12 16:24:00 +02:00
Add contains()
This commit is contained in:
18
README.md
18
README.md
@@ -24,6 +24,8 @@ A PHP library with a variety of string manipulation functions with multibyte sup
|
|||||||
* [endsWith](#endswith)
|
* [endsWith](#endswith)
|
||||||
* [toSpaces](#tospaces)
|
* [toSpaces](#tospaces)
|
||||||
* [toTabs](#totabs)
|
* [toTabs](#totabs)
|
||||||
|
* [slugify](#slugify)
|
||||||
|
* [contains](#contains)
|
||||||
* [Tests](#tests)
|
* [Tests](#tests)
|
||||||
* [License](#license)
|
* [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'
|
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
|
## TODO
|
||||||
|
|
||||||
**contains**
|
|
||||||
|
|
||||||
**between**
|
**between**
|
||||||
|
|
||||||
|
**surround**
|
||||||
|
|
||||||
**replace** => substr_replace
|
**replace** => substr_replace
|
||||||
|
|
||||||
**insert**
|
**insert**
|
||||||
@@ -337,6 +349,8 @@ S::slugify('Using strings like fòô bàř') // 'using-strings-like-foo-bar'
|
|||||||
|
|
||||||
**isJson**
|
**isJson**
|
||||||
|
|
||||||
|
**isMultibyte**
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
[](https://travis-ci.org/danielstjules/Stringy)
|
[](https://travis-ci.org/danielstjules/Stringy)
|
||||||
|
@@ -466,6 +466,23 @@ class Stringy {
|
|||||||
return self::dasherize(strtolower($str));
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
@@ -506,6 +506,32 @@ class StringyTestCase extends PHPUnit_Framework_TestCase {
|
|||||||
return $testData;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
Reference in New Issue
Block a user