mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-12 00:04:11 +02:00
Added $caseSensitive to contains()
This commit is contained in:
@@ -167,11 +167,13 @@ S::collapseWhitespace(' Ο συγγραφέας '); // 'Ο συγγρα
|
||||
|
||||
#### contains
|
||||
|
||||
$stringy->contains(string $needle)
|
||||
$stringy->contains(string $needle [, boolean $caseSensitive = true ])
|
||||
|
||||
S::contains(string $haystack, string $needle [, string $encoding ])
|
||||
S::contains(string $haystack, string $needle [, boolean $caseSensitive = true [, string $encoding ]])
|
||||
|
||||
Returns true if the string contains $needle, false otherwise.
|
||||
Returns true if the string contains $needle, false otherwise. By default,
|
||||
the comparison is case-sensitive, but can be made insensitive
|
||||
by setting $caseSensitive to false.
|
||||
|
||||
```php
|
||||
S::create('Ο συγγραφέας είπε', 'UTF-8')->contains('συγγραφέας');
|
||||
|
@@ -144,7 +144,7 @@ class StaticStringy
|
||||
* single space. This includes tabs and newline characters, as well as
|
||||
* multibyte whitespace such as the thin space and ideographic space.
|
||||
*
|
||||
* @param string $str The string to cleanup whitespace
|
||||
* @param string $str The string to cleanup whitespace
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The trimmed string with condensed whitespace
|
||||
*/
|
||||
@@ -311,16 +311,20 @@ class StaticStringy
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains $needle, false otherwise.
|
||||
* Returns true if the string contains $needle, false otherwise. By default,
|
||||
* the comparison is case-sensitive, but can be made insensitive by setting
|
||||
* $caseSensitive to false.
|
||||
*
|
||||
* @param string $haystack String being checked
|
||||
* @param string $needle Substring to look for
|
||||
* @param string $encoding The character encoding
|
||||
* @param string $haystack String being checked
|
||||
* @param string $needle Substring to look for
|
||||
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $haystack contains $needle
|
||||
*/
|
||||
public static function contains($haystack, $needle, $encoding = null)
|
||||
public static function contains($haystack, $needle, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($haystack, $encoding)->contains($needle);
|
||||
return Stringy::create($haystack, $encoding)->contains($needle, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -568,17 +568,20 @@ class Stringy
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains $needle, false otherwise.
|
||||
* Returns true if the string contains $needle, false otherwise. By default
|
||||
* the comparison is case-sensitive, but can be made insensitive by setting
|
||||
* $caseSensitive to false.
|
||||
*
|
||||
* @param string $needle Substring to look for
|
||||
* @param string $needle Substring to look for
|
||||
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
|
||||
* @return bool Whether or not $str contains $needle
|
||||
*/
|
||||
public function contains($needle)
|
||||
public function contains($needle, $caseSensitive = true)
|
||||
{
|
||||
if (mb_strpos($this->str, $needle, 0, $this->encoding) !== false)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
if ($caseSensitive)
|
||||
return (mb_strpos($this->str, $needle, 0, $this->encoding) !== false);
|
||||
else
|
||||
return (mb_stripos($this->str, $needle, 0, $this->encoding) !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -360,14 +360,24 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
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(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å´¥©', true, 'UTF-8'),
|
||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å˚ ∆', true, 'UTF-8'),
|
||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'øœ¬', 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')
|
||||
array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', true, 'UTF-8'),
|
||||
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßå˚', true, 'UTF-8'),
|
||||
array(true, 'This string contains foo bar', 'Foo bar', false),
|
||||
array(true, '12398!@(*%!@# @!%#*&^%', ' @!%#*&^%', false),
|
||||
array(true, 'Ο συγγραφέας είπε', 'ΣΥΓΓΡΑΦΈΑΣ', false, 'UTF-8'),
|
||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'Å´¥©', false, 'UTF-8'),
|
||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'Å˚ ∆', false, 'UTF-8'),
|
||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'ØŒ¬', false, 'UTF-8'),
|
||||
array(false, 'This string contains foo bar', 'foobar', false),
|
||||
array(false, 'This string contains foo bar', 'foo bar ', false),
|
||||
array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', false, 'UTF-8'),
|
||||
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßÅ˚', false, 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
|
@@ -235,9 +235,10 @@ class StaticStringyTestCase extends CommonTest
|
||||
/**
|
||||
* @dataProvider stringsForContains
|
||||
*/
|
||||
public function testContains($expected, $haystack, $needle, $encoding = null)
|
||||
public function testContains($expected, $haystack, $needle,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::contains($haystack, $needle, $encoding);
|
||||
$result = S::contains($haystack, $needle, $caseSensitive, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
@@ -300,10 +300,11 @@ class StringyTestCase extends CommonTest
|
||||
/**
|
||||
* @dataProvider stringsForContains
|
||||
*/
|
||||
public function testContains($expected, $haystack, $needle, $encoding = null)
|
||||
public function testContains($expected, $haystack, $needle,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($haystack, $encoding);
|
||||
$result = $stringy->contains($needle);
|
||||
$result = $stringy->contains($needle, $caseSensitive);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($haystack, $stringy);
|
||||
|
Reference in New Issue
Block a user