Edit on GitHub
Jump to docs navigation

Twig Components / Methods / preg_split

preg_split(pattern) is a Twig filter to make PHPs preg_split() function available as Twig filter. It splits text into an array using a regular expression.

{% set keywords = "hypertext language, programming" %}
{{ keywords|preg_split("/[\s,]+/" }}

The result will be:

[0] => hypertext
[1] => language
[2] => programming

Jasny's Twig Extensions can be easily installed using Composer:

composer require jasny/twig-extensions


$twig = new Twig_Environment($loader, $options);
$twig->addExtension(new Jasny\Twig\ArrayExtension());

To use in a Symfony project register the extensions as a service:

class: Jasny\Twig\ArrayExtension
- { name: twig.extension }

Source: Jasny

Edit this page on GitHub
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.