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

Added indexOf and indexOfLast

This commit is contained in:
Tadeu Bento
2015-04-09 23:15:13 +01:00
committed by Daniel St. Jules
parent 3cf18e9e42
commit 9a5601efef
6 changed files with 144 additions and 1 deletions

View File

@@ -29,6 +29,8 @@ PHP 5.3+ and HHVM. Inspired by underscore.string.js.
* [hasLowerCase](#haslowercase) * [hasLowerCase](#haslowercase)
* [hasUpperCase](#hasuppercase) * [hasUpperCase](#hasuppercase)
* [humanize](#humanize) * [humanize](#humanize)
* [indexOf](#indexof)
* [indexOfLast](#indexoflast)
* [insert](#insert) * [insert](#insert)
* [isAlpha](#isalpha) * [isAlpha](#isalpha)
* [isAlphanumeric](#isalphanumeric) * [isAlphanumeric](#isalphanumeric)
@@ -447,6 +449,34 @@ S::create('author_id')->humanize();
S::humanize('author_id'); // 'Author' S::humanize('author_id'); // 'Author'
``` ```
#### indexOf
$stringy->indexOf(string $substr [, $offset = 0 ]);
S::indexOf(string $str , string $substr [, $offset = 0 [, $encoding = null ]])
Returns the offset/index of the first occurrence of $substr in the string.
In case $substr is not a substring of the string, returns false.
```php
S::create('string', 'UTF-8')->indexOf('ing');
S::indexOf('string', 'ing', 0, 'UTF-8'); // 3
```
#### indexOfLast
$stringy->indexOfLast(string $substr [, $offset = 0 ]);
S::indexOfLast(string $str , string $substr [, $offset = 0 [, $encoding = null ]])
Returns the offset/index of the last occurrence of $substr in the string.
In case $substr is not a substring of the string, returns false.
```php
S::create('string', 'UTF-8')->indexOfLast('ing');
S::indexOfLast('string string', 'ing', 0, 'UTF-8'); // 10
```
#### insert #### insert
$stringy->insert(int $index, string $substring) $stringy->insert(int $index, string $substring)

View File

@@ -421,6 +421,34 @@ class StaticStringy
->containsAll($needles, $caseSensitive); ->containsAll($needles, $caseSensitive);
} }
/**
* Returns the offset/index of the first occurance of $substr in the string.
* In case $substr is not a substring of the string, returns false.
*
* @param string $str The haystack to search through
* @param string $substr substring
* @param int $offset
* @return int|bool
*/
public static function indexOf($str, $substr, $offset = 0, $encoding = null)
{
return Stringy::create($str, $encoding)->indexOf($substr, $offset);
}
/**
* Returns the offset/index of the last occurance of $substr in the string.
* In case $substr is not a substring of the string, returns false.
*
* @param string $str The haystack to search through
* @param string $substr substring
* @param int $offset
* @return int|bool
*/
public static function indexOfLast($str, $substr, $offset = 0, $encoding = null)
{
return Stringy::create($str, $encoding)->indexOfLast($substr, $offset);
}
/** /**
* Surrounds a string with the given substring. * Surrounds a string with the given substring.
* *

View File

@@ -520,7 +520,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ', 'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ', 'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ',
'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А'), 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А'),
'B' => array('Б', 'Β'), 'B' => array('Б', 'Β'),
'C' => array('Ć', 'Č', 'Ĉ', 'Ċ'), 'C' => array('Ç','Ć', 'Č', 'Ĉ', 'Ċ'),
'D' => array('Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', 'Δ'), 'D' => array('Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', 'Δ'),
'E' => array('É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể', 'Ễ', 'E' => array('É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể', 'Ễ',
'Ệ', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', 'Ἐ', 'Ệ', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', 'Ἐ',
@@ -887,6 +887,32 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
return true; return true;
} }
/**
* Returns the offset/index of the first occurrence of $substr in the string.
* In case $substr is not a substring of the string, returns false.
*
* @param string $substr substring
* @param int $offset
* @return int|bool
*/
public function indexOf($substr, $offset = 0)
{
return mb_strpos($this->str, (string)$substr, (int)$offset, $this->encoding);
}
/**
* Returns the offset/index of the last occurrence of $substr in the string.
* In case $substr is not a substring of the string, returns false.
*
* @param string $substr substring
* @param int $offset
* @return int|bool
*/
public function indexOfLast($substr, $offset = 0)
{
return mb_strrpos($this->str, (string)$substr, (int)$offset, $this->encoding);
}
/** /**
* Surrounds $str with the given substring. * Surrounds $str with the given substring.
* *

View File

@@ -14,6 +14,26 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
$this->assertInstanceOf('Stringy\Stringy', $actual); $this->assertInstanceOf('Stringy\Stringy', $actual);
} }
public function indexOfProvider()
{
return array(
array(2, 'This is the string', 'is'),
array(2, 'This is the string', 'is', 0, 'UTF-8'),
array(false, 'This is the string', 'not-found', 0, 'UTF-8'),
array(32, 'This is the string... and there is another thing', 'is', 10, 'UTF-8'),
);
}
public function indexOfLastProvider()
{
return array(
array(5, 'This is the string', 'is'),
array(5, 'This is the string', 'is', 0, 'UTF-8'),
array(false, 'This is the string', 'not-found', 0, 'UTF-8'),
array(32, 'This is the string... and there is another thing', 'is', 0, 'UTF-8'),
);
}
public function charsProvider() public function charsProvider()
{ {
return array( return array(
@@ -1023,4 +1043,5 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array('fòô', 'bàř', '[[:alpha:]]{3}', 'fòô', 'msr', 'UTF-8') array('fòô', 'bàř', '[[:alpha:]]{3}', 'fòô', 'msr', 'UTF-8')
); );
} }
} }

View File

@@ -7,6 +7,24 @@ use Stringy\StaticStringy as S;
class StaticStringyTestCase extends CommonTest class StaticStringyTestCase extends CommonTest
{ {
/** /**
* @dataProvider indexOfProvider()
*/
public function testIndexOf($expected, $str, $subStr, $offset = 0, $encoding = null)
{
$result = S::indexOf($str, $subStr, $offset, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider indexOfLastProvider()
*/
public function testIndexOfLast($expected, $str, $subStr, $offset = 0, $encoding = null)
{
$result = S::indexOfLast($str, $subStr, $offset, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider charsProvider() * @dataProvider charsProvider()
*/ */
public function testChars($expected, $str, $encoding = null) public function testChars($expected, $str, $encoding = null)
@@ -647,4 +665,5 @@ class StaticStringyTestCase extends CommonTest
$this->assertInternalType('string', $result); $this->assertInternalType('string', $result);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
} }

View File

@@ -153,6 +153,24 @@ class StringyTestCase extends CommonTest
unset($stringy[1]); unset($stringy[1]);
} }
/**
* @dataProvider indexOfProvider()
*/
public function testIndexOf($expected, $str, $subStr, $offset = 0, $encoding = null)
{
$result = S::create($str, $encoding)->indexOf($subStr, $offset);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider indexOfLastProvider()
*/
public function testIndexOfLast($expected, $str, $subStr, $offset = 0, $encoding = null)
{
$result = S::create($str, $encoding)->indexOfLast($subStr, $offset);
$this->assertEquals($expected, $result);
}
/** /**
* @dataProvider charsProvider() * @dataProvider charsProvider()
*/ */
@@ -911,4 +929,5 @@ class StringyTestCase extends CommonTest
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy); $this->assertEquals($str, $stringy);
} }
} }