mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 03:46:39 +00:00
Fix MB characters handling in split
This commit is contained in:
committed by
Fabien Potencier
parent
9fb15e343e
commit
8cde52d222
@@ -173,7 +173,7 @@ class Twig_Extension_Core extends Twig_Extension
|
||||
|
||||
// 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'),
|
||||
@@ -796,9 +796,27 @@ function twig_join_filter($value, $glue = '')
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
{{ foo|split(',')|join('-') }}
|
||||
{{ foo|split(',', 3)|join('-') }}
|
||||
{{ baz|split('')|join('-') }}
|
||||
{{ baz|split('', 1)|join('-') }}
|
||||
{{ baz|split('', 2)|join('-') }}
|
||||
{{ foo|split(',', -2)|join('-') }}
|
||||
--DATA--
|
||||
@@ -14,5 +15,6 @@ one-two-three-four-five
|
||||
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
|
||||
@@ -0,0 +1,24 @@
|
||||
--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
|
||||
Reference in New Issue
Block a user