// array helpers
new Twig_SimpleFilter('join', 'twig_join_filter'),
- new Twig_SimpleFilter('split', 'twig_split_filter'),
+ new Twig_SimpleFilter('split', 'twig_split_filter', array('needs_environment' => true)),
new Twig_SimpleFilter('sort', 'twig_sort_filter'),
new Twig_SimpleFilter('merge', 'twig_array_merge'),
new Twig_SimpleFilter('batch', 'twig_array_batch'),
*
* @return array The split string as an array
*/
-function twig_split_filter($value, $delimiter, $limit = null)
+function twig_split_filter(Twig_Environment $env, $value, $delimiter, $limit = null)
{
if (empty($delimiter)) {
+ if (function_exists('mb_get_info') && null !== $charset = $env->getCharset()) {
+ if ($limit > 1) {
+ $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;
+ }
+
+ return preg_split('/(?<!^)(?!$)/u', $value);
+ }
+
return str_split($value, null === $limit ? 1 : $limit);
}
{{ foo|split(',')|join('-') }}
{{ foo|split(',', 3)|join('-') }}
{{ baz|split('')|join('-') }}
+{{ baz|split('', 1)|join('-') }}
{{ baz|split('', 2)|join('-') }}
{{ foo|split(',', -2)|join('-') }}
--DATA--
one-two-three-four-five
one-two-three,four,five
1-2-3-4-5
+1-2-3-4-5
12-34-5
one-two-three
\ No newline at end of file
--- /dev/null
+--TEST--
+"split" filter
+--CONDITION--
+function_exists('mb_get_info')
+--TEMPLATE--
+{{ "é"|split('', 10)|join('-') }}
+{{ foo|split(',')|join('-') }}
+{{ foo|split(',', 1)|join('-') }}
+{{ foo|split(',', 2)|join('-') }}
+{{ foo|split(',', 3)|join('-') }}
+{{ baz|split('')|join('-') }}
+{{ baz|split('', 1)|join('-') }}
+{{ baz|split('', 2)|join('-') }}
+--DATA--
+return array('foo' => 'Ä,é,Äほ', 'baz' => 'éÄßごa',)
+--EXPECT--
+é
+Ä-é-Äほ
+Ä,é,Äほ
+Ä-é,Äほ
+Ä-é-Äほ
+é-Ä-ß-ご-a
+é-Ä-ß-ご-a
+éÄ-ßご-a
\ No newline at end of file