1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-09-02 01:22:37 +02:00

Added slice

This commit is contained in:
Daniel St. Jules
2015-07-25 12:59:23 -07:00
parent 342ef7c3dd
commit 5e3ac6231c
3 changed files with 74 additions and 0 deletions

View File

@@ -1305,6 +1305,33 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
return mb_strlen($this->str, $this->encoding);
}
/**
* Returns the substring beginning at $start, and up to, but not including
* the index specified by $end. If $end is omitted, the function extracts
* the remaining string. If $end is negative, it is computed from the end
* of the string.
*
* @param int $start Initial index from which to begin extraction
* @param int $end Optional index at which to end extraction
* @return Stringy Object with its $str being the extracted substring
*/
public function slice($start, $end = null)
{
if ($end === null) {
$length = $this->length();
} elseif ($end >= 0 && $end <= $start) {
return static::create('', $this->encoding);
} elseif ($end < 0) {
$length = $this->length() + $end - $start;
} else {
$length = $end - $start;
}
$str = mb_substr($this->str, $start, $length, $this->encoding);
return static::create($str, $this->encoding);
}
/**
* Returns the substring beginning at $start with the specified $length.
* It differs from the mb_substr() function in that providing a $length of