Slices a variable.
Twig_Environment $env A Twig_Environment instance:
mixed $item A variable:
integer $start Start of the slice:
integer $length Size of the slice:
Boolean $preserveKeys Whether to preserve key or not (when the input is an array):
mixed The sliced variable
function twig_slice(Twig_Environment $env, $item, $start, $length = null, $preserveKeys = false) {
if ($item instanceof Traversable) {
$item = iterator_to_array($item, false);
}
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 mb_substr($item, $start, null === $length ? mb_strlen($item, $charset) - $start : $length, $charset);
}
return null === $length ? substr($item, $start) : substr($item, $start, $length);
}