Twig split filter

 The split filter splits a string by the given delimiter and returns a list of strings.

If you want to split a url , use split filter, suppose you have a url public://Quarterly/2019-07/sample.pdf.

And you want to print only 2019-07 , you can use split filter 

Example 

{% set foo = "public://Quarterly/2019-07/sample.pdf"|split('/') %}

{# foo contains ['public:', '', 'Quarterly' , '2019-07','sample.pdf'] #}

for printing 2019-09  ,  use {{ foo.3 }}

You can also pass a limit argument

A- If limit is positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string;

B- If limit is negative, all components except the last -limit are returned;

C- If limit is zero, then this is treated as 1.


{% set foo = "one,two,three,four,five"|split(',', 3) %}

{# foo contains ['one', 'two', 'three,four,five'] #}

If the delimiter is an empty string, then value will be split by equal chunks. Length is set by the limit argument (one character by default).


{% set foo = "123"|split('') %}

{# foo contains ['1', '2', '3'] #}


{% set bar = "aabbcc"|split('', 2) %}

{# bar contains ['aa', 'bb', 'cc'] #}

No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...