Skip to content
  • Homepage
  • HTML
  • CSS
  • Symfony
  • PHP
  • How to
  • Contact
  • Donate

Teach Developer

Articles, Guides & Tips

How to split a string into an array with Twig

Home  »  Symfony • PHP   »   How to split a string into an array with Twig
Posted on March 14, 2022July 19, 2022 6 Comments on How to split a string into an array with Twig
915

Twig provides many filters that mimic the basic features of PHP that are easy to understand for front-end developers.

One of those filters is a split filter that allows you to split a delimited string by a single character, returning a repeating array.

{% set tags = "First, Second, Third" | split(",") %}
{# tags contains ['First', 'Second', 'Third'] #}

{# Print every tag in a new line #}
{% for tag in tags %}
    {{ tag }}
{% endfor %}

You can limit the length of the result array providing it as second argument, for example, to limit the length of the array to only 3 items:

{% set tags = "first,second,third,fourth,fifth"|split(',', 3) %}
{# tags contains ["first", "second", "third,fourth,fifth"]#}

{% for tag in tags %}
    {{ tag }}
{% endfor %}

If the delimiter is empty, the string will be split into chunks of the same size:

{% set tags = "abcd"|split('') %}
{# tags contains ["a", "b", "c", "d"]#}

{% for tag in tags %}
    {{ tag }}
{% endfor %}

Whenever there’s no delimiter (an empty string), the limit argument will specify the size of the chunk:

{% set tags = "aabbccdd"|split('', 2) %}
{# tags contains ["aa", "bb", "cc", "dd"]#}

{% for tag in tags %}
    {{ tag }}
{% endfor %}
Symfony, PHP Tags:Symfony

Post navigation

Previous Post: CSS
Next Post: Common Mistakes with Conditionals

Related Posts

  • How to Symfony Request
  • How to remove index.php from URL in CodeIgniter 4
  • How To Install Nginx, PHP on Ubuntu 22.04
  • How to Check Twig Version in Symfony
  • Common Mistakes with Conditionals
  • PHP 8: Constructor property promotion

6 Comments on “How to split a string into an array with Twig”

  1. Cheapest eBooks Store says:
    July 17, 2022 at 7:45 pm

    Hello, I enjoy reading through your article. I like to write a little comment to support you.

    Reply
    1. TeachDeveloper says:
      July 19, 2022 at 3:47 am

      Thanks

      Reply
  2. Lucashmsilva says:
    July 24, 2022 at 6:41 am

    “Great content as always! Thanks a lot.”

    Reply
  3. Rakibul Haq says:
    July 24, 2022 at 6:46 am

    “This is what exactly I was looking for🙏
    Thanks a lot buddy for sharing.,its really easy to understand and implement.”

    Reply
  4. Alex Xu says:
    July 24, 2022 at 6:52 am

    Very detailed explanation. Thanks for sharing it.

    Reply
  5. Imanali Mamadiev says:
    July 24, 2022 at 6:57 am

    I love this articles. Thanks for sharing.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Codeigniter (3)
  • CSS (11)
  • eCommerce (1)
  • Framework (1)
  • Git (3)
  • How to (43)
  • HTML (5)
  • JavaScript (15)
  • Jquery (7)
  • Laravel (1)
  • Linux (4)
  • Magento-2 (1)
  • Node js (4)
  • Others (2)
  • PHP (11)
  • React (13)
  • Server (1)
  • SSH (3)
  • Symfony (6)
  • Tips (16)
  • Top Tutorials (10)
  • Ubuntu (3)
  • Vue (1)
  • Wordpress (7)

Latest Posts

  • What is SSH in Linux?
  • How to Delete Files in Ubuntu Command Line
  • How to Deploy a React application on a cPanel
  • How to use events listeners and Event Subscriber in Symfony
  • How to Convert PHP CSV to JSON

WEEKLY TAGS

AJAX (1) Codeigniter (1) Javascript (11) JQuery (1) PHP (16) Programming (1) React (3) Symfony (1)

Random Post

How to style an HTML radio button to look like a checkbox with CSS?
How to check if a customer is logged in to Magento 2 or not?
How to remove index.php from URL in CodeIgniter 4
How to use setTimeout and setInterval methods in JavaScript
How to Add Reset Button in jQuery UI Datepicker

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved