function twig_split_filter in Translation template extractor 7.3
Same name and namespace in other branches
- 6.3 vendor/Twig/Extension/Core.php \twig_split_filter()
Splits the string into an array.
<pre> {{ "one,two,three"|split(',') }} {# returns [one, two, three] #}
{{ "one,two,three,four,five"|split(',', 3) }} {# returns [one, two, "three,four,five"] #}
{{ "123"|split('') }} {# returns [1, 2, 3] #}
{{ "aabbcc"|split('', 2) }} {# returns [aa, bb, cc] #} </pre>
Parameters
Twig_Environment $env A Twig_Environment instance:
string $value A string:
string $delimiter The delimiter:
int $limit The limit:
Return value
array The split string as an array
1 string reference to 'twig_split_filter'
- 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 827
Code
function twig_split_filter(Twig_Environment $env, $value, $delimiter, $limit = null) {
if (!empty($delimiter)) {
return null === $limit ? explode($delimiter, $value) : explode($delimiter, $value, $limit);
}
if (!function_exists('mb_get_info') || null === ($charset = $env
->getCharset())) {
return str_split($value, null === $limit ? 1 : $limit);
}
if ($limit <= 1) {
return preg_split('/(?<!^)(?!$)/u', $value);
}
$length = mb_strlen($value, $charset);
if ($length < $limit) {
return array(
$value,
);
}
$r = array();
for ($i = 0; $i < $length; $i += $limit) {
$r[] = mb_substr($value, $i, $limit, $charset);
}
return $r;
}