*/
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);
- }
+ if (!empty($delimiter)) {
+ return null === $limit ? explode($delimiter, $value) : explode($delimiter, $value, $limit);
+ }
- $r = array();
- for ($i = 0; $i < $length; $i += $limit) {
- $r[] = mb_substr($value, $i, $limit, $charset);
- }
+ if (!function_exists('mb_get_info') || null === $charset = $env->getCharset()) {
+ return str_split($value, null === $limit ? 1 : $limit);
+ }
- return $r;
- }
+ if ($limit <= 1) {
+ return preg_split('/(?<!^)(?!$)/u', $value);
+ }
- return preg_split('/(?<!^)(?!$)/u', $value);
- }
+ $length = mb_strlen($value, $charset);
+ if ($length < $limit) {
+ return array($value);
+ }
- return str_split($value, null === $limit ? 1 : $limit);
+ $r = array();
+ for ($i = 0; $i < $length; $i += $limit) {
+ $r[] = mb_substr($value, $i, $limit, $charset);
}
- return null === $limit ? explode($delimiter, $value) : explode($delimiter, $value, $limit);
+ return $r;
}
// The '_default' filter is used internally to avoid using the ternary operator