-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -430,6 +430,71 @@ public static function random($length = 10, $charlist = '0-9a-z') | |
} | ||
|
||
|
||
/** | ||
* Returns part of $haystack before $nth occurence of $needle. | ||
* @param string | ||
* @param string | ||
* @param int negative value means searching from the end | ||
* @return string|FALSE returns FALSE if the needle was not found | ||
*/ | ||
public static function before($haystack, $needle, $nth = 1) | ||
{ | ||
$pos = self::pos($haystack, $needle, $nth); | ||
return $pos === FALSE | ||
? FALSE | ||
: substr($haystack, 0, $pos); | ||
} | ||
|
||
|
||
/** | ||
* Returns part of $haystack after $nth occurence of $needle. | ||
* @param string | ||
* @param string | ||
* @param int negative value means searching from the end | ||
* @return string|FALSE returns FALSE if the needle was not found | ||
*/ | ||
public static function after($haystack, $needle, $nth = 1) | ||
{ | ||
$pos = self::pos($haystack, $needle, $nth); | ||
return $pos === FALSE | ||
? FALSE | ||
: (string) substr($haystack, $pos + strlen($needle)); | ||
} | ||
|
||
|
||
/** | ||
* Returns position of $nth occurence of $needle in $haystack. | ||
* @param string | ||
* @param string | ||
* @param int negative value means searching from the end | ||
This comment has been minimized.
Sorry, something went wrong. |
||
* @return string|FALSE returns FALSE if the needle was not found | ||
*/ | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
JanTvrdik
Contributor
|
||
public static function pos($haystack, $needle, $nth = 1) | ||
{ | ||
if (!$nth) { | ||
return FALSE; | ||
} elseif ($nth > 0) { | ||
if (strlen($needle) === 0) { | ||
return 0; | ||
} | ||
$pos = 0; | ||
while (FALSE !== ($pos = strpos($haystack, $needle, $pos)) && --$nth) { | ||
$pos++; | ||
} | ||
} else { | ||
$len = strlen($haystack); | ||
if (strlen($needle) === 0) { | ||
return $len; | ||
} | ||
$pos = $len - 1; | ||
while (FALSE !== ($pos = strrpos($haystack, $needle, $pos - $len)) && ++$nth) { | ||
$pos--; | ||
} | ||
} | ||
return $pos; | ||
} | ||
|
||
|
||
/** | ||
* Splits string by a regular expression. | ||
* @param string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Utils\Strings::after() | ||
*/ | ||
|
||
use Nette\Utils\Strings, | ||
Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
|
||
test(function () { //after | ||
$foo = '0123456789a123456789b123456789c'; | ||
Assert::same('123456789a123456789b123456789c', Strings::after($foo, '0', 1)); | ||
Assert::same('a123456789b123456789c', Strings::after($foo, '9', 1)); | ||
Assert::same('a123456789b123456789c', Strings::after($foo, '789', 1)); | ||
Assert::same('0123456789a123456789b123456789c', Strings::after($foo, '', 1)); | ||
Assert::same('', Strings::after($foo, '', -1)); | ||
Assert::same('', Strings::after($foo, 'c', -1)); | ||
Assert::same('c', Strings::after($foo, '9', -1)); | ||
Assert::same('c', Strings::after($foo, '789', -1)); | ||
Assert::same('c', Strings::after($foo, '9', 3)); | ||
Assert::same('c', Strings::after($foo, '789', 3)); | ||
Assert::same('a123456789b123456789c', Strings::after($foo, '9', -3)); | ||
Assert::same('a123456789b123456789c', Strings::after($foo, '789', -3)); | ||
Assert::false(Strings::after($foo, '9', 0)); | ||
Assert::false(Strings::after($foo, 'not-in-string')); | ||
Assert::false(Strings::after($foo, 'b', -2)); | ||
Assert::false(Strings::after($foo, 'b', 2)); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Utils\Strings::before() | ||
*/ | ||
|
||
use Nette\Utils\Strings, | ||
Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
|
||
test(function () { //before | ||
$foo = '0123456789a123456789b123456789c'; | ||
Assert::same('', Strings::before($foo, '0', 1)); | ||
Assert::same('012345678', Strings::before($foo, '9', 1)); | ||
Assert::same('0123456', Strings::before($foo, '789', 1)); | ||
Assert::same('', Strings::before($foo, '', 1)); | ||
Assert::same('0123456789a123456789b123456789c', Strings::before($foo, '', -1)); | ||
Assert::same('0123456789a123456789b123456789', Strings::before($foo, 'c', -1)); | ||
Assert::same('0123456789a123456789b123456', Strings::before($foo, '789', -1)); | ||
Assert::same('0123456789a123456789b12345678', Strings::before($foo, '9', 3)); | ||
Assert::same('0123456789a123456789b123456', Strings::before($foo, '789', 3)); | ||
Assert::same('012345678', Strings::before($foo, '9', -3)); | ||
Assert::same('0123456', Strings::before($foo, '789', -3)); | ||
Assert::false(Strings::before($foo, '9', 0)); | ||
Assert::false(Strings::before($foo, 'not-in-string')); | ||
Assert::false(Strings::before($foo, 'b', -2)); | ||
Assert::false(Strings::before($foo, 'b', 2)); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Utils\Strings::pos() | ||
*/ | ||
|
||
use Nette\Utils\Strings, | ||
Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
|
||
test(function () { //after | ||
This comment has been minimized.
Sorry, something went wrong. |
||
$foo = '0123456789a123456789b123456789c'; | ||
Assert::same(0, Strings::pos($foo, '0', 1)); | ||
Assert::same(9, Strings::pos($foo, '9', 1)); | ||
Assert::same(7, Strings::pos($foo, '789', 1)); | ||
Assert::same(0, Strings::pos($foo, '', 1)); | ||
Assert::same(31, Strings::pos($foo, '', -1)); | ||
Assert::same(30, Strings::pos($foo, 'c', -1)); | ||
Assert::same(29, Strings::pos($foo, '9', -1)); | ||
Assert::same(27, Strings::pos($foo, '789', -1)); | ||
Assert::same(29, Strings::pos($foo, '9', 3)); | ||
Assert::same(27, Strings::pos($foo, '789', 3)); | ||
Assert::same(9, Strings::pos($foo, '9', -3)); | ||
Assert::same(7, Strings::pos($foo, '789', -3)); | ||
Assert::false(Strings::pos($foo, '9', 0)); | ||
Assert::false(Strings::pos($foo, 'not-in-string')); | ||
Assert::false(Strings::pos($foo, 'b', -2)); | ||
Assert::false(Strings::pos($foo, 'b', 2)); | ||
}); |
1 comment
on commit 8c57c61
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 nice, these will be useful :-)
Incorrect
@return
annotation.