Stringy\Stringy::split PHP Method

split() public method

Splits the string with the provided regular expression, returning an array of Stringy objects. An optional integer $limit will truncate the results.
public split ( string $pattern, integer $limit = null ) : Stringy[]
$pattern string The regex with which to split the string
$limit integer Optional maximum number of results to return
return Stringy[] An array of Stringy objects
    public function split($pattern, $limit = null)
    {
        if ($limit === 0) {
            return array();
        }
        // mb_split errors when supplied an empty pattern in < PHP 5.4.13
        // and HHVM < 3.8
        if ($pattern === '') {
            return array(static::create($this->str, $this->encoding));
        }
        $regexEncoding = $this->regexEncoding();
        $this->regexEncoding($this->encoding);
        // mb_split returns the remaining unsplit string in the last index when
        // supplying a limit
        $limit = $limit > 0 ? $limit += 1 : -1;
        static $functionExists;
        if ($functionExists === null) {
            $functionExists = function_exists('\\mb_split');
        }
        if ($functionExists) {
            $array = \mb_split($pattern, $this->str, $limit);
        } else {
            if ($this->supportsEncoding()) {
                $array = \preg_split("/{$pattern}/", $this->str, $limit);
            }
        }
        $this->regexEncoding($regexEncoding);
        if ($limit > 0 && count($array) === $limit) {
            array_pop($array);
        }
        for ($i = 0; $i < count($array); $i++) {
            $array[$i] = static::create($array[$i], $this->encoding);
        }
        return $array;
    }