You are here

function twig_slice in Translation template extractor 6.3

Same name and namespace in other branches
  1. 7.3 vendor/Twig/Extension/Core.php \twig_slice()

Slices a variable.

Parameters

Twig_Environment $env A Twig_Environment instance:

mixed $item A variable:

int $start Start of the slice:

int $length Size of the slice:

bool $preserveKeys Whether to preserve key or not (when the input is an array):

Return value

mixed The sliced variable

2 calls to twig_slice()
twig_first in vendor/Twig/Extension/Core.php
Returns the first element of the item.
twig_last in vendor/Twig/Extension/Core.php
Returns the last element of the item.
1 string reference to 'twig_slice'
Twig_Extension_Core::getFilters in vendor/Twig/Extension/Core.php
Returns a list of filters to add to the existing list.

File

vendor/Twig/Extension/Core.php, line 703

Code

function twig_slice(Twig_Environment $env, $item, $start, $length = null, $preserveKeys = false) {
  if ($item instanceof Traversable) {
    if ($item instanceof IteratorAggregate) {
      $item = $item
        ->getIterator();
    }
    if ($start >= 0 && $length >= 0) {
      return iterator_to_array(new LimitIterator($item, $start, $length === null ? -1 : $length), $preserveKeys);
    }
    $item = iterator_to_array($item, $preserveKeys);
  }
  if (is_array($item)) {
    return array_slice($item, $start, $length, $preserveKeys);
  }
  $item = (string) $item;
  if (function_exists('mb_get_info') && null !== ($charset = $env
    ->getCharset())) {
    return (string) mb_substr($item, $start, null === $length ? mb_strlen($item, $charset) - $start : $length, $charset);
  }
  return (string) (null === $length ? substr($item, $start) : substr($item, $start, $length));
}