* str_split on empty delimeter.
* tests and documentation updated.
``split``
========
-The ``split`` filter returns a list of items from a string that's separated by the provided delimiter or glue:
+The ``split`` filter returns a list of items from a string that's separated by the provided delimiter:
.. code-block:: jinja
{{ "one,two,three"|split(',') }}
{# returns [one, two, three] #}
-A limit parameter is available which the returned list will contain a maximum of limit elements with the last element containing the rest of string.
+A limit parameter is available which the returned list will contain a maximum of limit elements with the last element containing the rest of string.
+If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
+If the limit parameter is negative, all components except the last -limit are returned.
+If the limit parameter is zero, then this is treated as 1.
.. code-block:: jinja
{{ "one,two,three,four,five"|split(',', 3) }}
{# returns [one, two, "three,four,five"] #}
+
+If delimiter is an empty string, then value will be splitted by equal chunks. Length is set by limit parameter (1 char by default).
+
+.. code-block:: jinja
+
+ {{ "123"|split('') }}
+ {# returns [1, 2, 3] #}
+
+ {{ "aabbcc"|split('', 2) }}
+ {# returns [aa, bb, cc] #}
+
+.. note::
+
+ Internally, Twig uses the PHP `explode`_ or `str_split`_ (if delimiter is empty) functions for string splitting.
+
+.. _`explode`: http://php.net/explode
+
+.. _`str_split`: http://php.net/str_split
}
/**
- * Splits the values into an array.
+ * Splits the string into an array.
*
* The second parameter is option for the limit.
+ * If delimiter is an empty string, then string is split by equal chunks. Chunk length is 1 char by default, or set
+ * by limit
*
* <pre>
* {{ "one,two,three"|split(',') }}
*
* {{ "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>
*
- * @param string $value A string
+ * @param string $value A string
* @param string $delimiter The separator to explode by
- * @param integer $limit The limit
+ * @param integer $limit The limit
*
* @return string The explode'ed string
*/
function twig_split_filter($value, $delimiter, $limit = null)
{
- if (is_empty($delimiter)) {
- throw new Twig_Error_Runtime('The string to split must be provided with a delimiter.');
- }
-
- if (!is_null($limit)) {
- return explode($delimiter, $value, $limit);
+ if (empty($delimiter)) {
+ return str_split($value, null === $limit ? 1 : $limit);
}
- return explode($delimiter, $value);
+ return null === $limit ? explode($delimiter, $value) : explode($delimiter, $value, $limit);
}
// The '_default' filter is used internally to avoid using the ternary operator
--TEMPLATE--
{{ "one,two,three,four,five"|split(',')|join('-') }}
{{ foo|split(',')|join('-') }}
-{{ bar|split(',', 3)|join('-') }}
+{{ foo|split(',', 3)|join('-') }}
+{{ baz|split('')|join('-') }}
+{{ baz|split('', 2)|join('-') }}
+{{ foo|split(',', -2)|join('-') }}
--DATA--
-return array('foo' => "one,two,three,four,five", 'bar' => "one,two,three,four,five")
+return array('foo' => "one,two,three,four,five", 'baz' => '12345',)
--EXPECT--
one-two-three-four-five
one-two-three-four-five
one-two-three,four,five
+1-2-3-4-5
+12-34-5
+one-two-three
\ No newline at end of file