Skip to content

Commit

Permalink
Adding string contains utility
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-aranda committed Jun 28, 2015
1 parent d83d411 commit 21de9a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/PHPRocks/Util/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,17 @@ public static function underscoreToCamelCase($string){
}, $string);
}

/**
* @param $string
* @param $needle
* @return bool
*/
public static function contains($string, $needle, $ignore_case = false){
if( $ignore_case ){
return stripos($string, $needle) !== false;
}else{
return strpos($string, $needle) !== false;
}
}

}
9 changes: 9 additions & 0 deletions tests/PHPRocks/Util/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@ public function testUnderscoreToCamelCase() {
$this->assertSame('DanielTest2', String::underscoreToCamelCase('daniel_test_2'));
}

public function testContains() {
$this->assertTrue(String::contains('daniel test', 'test'));
$this->assertTrue(String::contains('daniel test', 'daniel'));
$this->assertTrue(String::contains('daniel test', ' '));
$this->assertFalse(String::contains('some test', 'daniel'));
$this->assertFalse(String::contains('some test', 'Test'));
$this->assertTrue(String::contains('some test', 'Test', true));
}

}

0 comments on commit 21de9a3

Please sign in to comment.