PHPackages                             falc/twig-text-extension - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. falc/twig-text-extension

ActiveLibrary[Templating &amp; Views](/categories/templating)

falc/twig-text-extension
========================

Twig text extension.

v0.2.0(10y ago)390.6k1MITPHP

Since Jun 11Pushed 10y ago2 watchersCompare

[ Source](https://github.com/Falc/TwigTextExtension)[ Packagist](https://packagist.org/packages/falc/twig-text-extension)[ RSS](/packages/falc-twig-text-extension/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

Twig Text Extension
===================

[](#twig-text-extension)

TwigTextExtension contains the following filters:

- `br2p`: Replaces double linebreaks formatting into paragraphs.
- `hash`: Exposes the [hash](http://www.php.net/manual/en/function.hash.php) function included in PHP.
- `p2br`: Replaces paragraph formatting with double linebreaks.
- `paragraphs_slice`: Extracts paragraphs from a string. Similar to [array\_slice](http://www.php.net/manual/en/function.array-slice.php).
- `regex_replace`: Exposes the [preg\_replace](http://php.net/manual/en/function.preg-replace.php) function included in PHP.
- `repeat`: Exposes the [str\_repeat](http://php.net/manual/en/function.str-repeat.php) function included in PHP.

License: **MIT**

Requirements
------------

[](#requirements)

- Twig &gt;= 1.12

Installation
------------

[](#installation)

Add the `falc/twig-text-extension` package to your `composer.json`:

```
composer require falc/twig-text-extension

```

### Symfony

[](#symfony)

To use it in a Symfony project you have to [register the extension as a service](http://symfony.com/doc/current/cookbook/templating/twig_extension.html#register-an-extension-as-a-service).

```
services:
    twig.extension.text:
        class: Falc\Twig\Extension\TextExtension
        tags:
            - { name: twig.extension }
```

If there is already an extension registered as `twig.extension.text` you can give the new one a different name:

```
services:
    twig.extension.text:
        class: Other\Twig\Extension\TextExtension
        tags:
            - { name: twig.extension }
    twig.extension.falc_text:
        class: Falc\Twig\Extension\TextExtension
        tags:
            - { name: twig.extension }
```

### Other

[](#other)

To use it in other projects:

```
$twig = new Twig_Environment($loader, $options);
$twig->addExtension(new Falc\Twig\Extension\TextExtension());
```

Usage
-----

[](#usage)

### br2p

[](#br2p)

```
// Example
{{ 'This is a text.This should be another paragraph.' | br2p }}

// Output
"This is a text.This should be another paragraph."

```

### hash(algorithm, rawOutput)

[](#hashalgorithm-rawoutput)

- `algorithm`: Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..)
- `rawOutput`: When set to `true`, outputs raw binary data. `False` outputs lowercase hexits.
    - Optional.
    - Default is **false**.

```
// Example
{{ 'hash-something' | hash('md5') }}

// Output
"6885610d9373d81639f73b6844aad6b3"

```

Check [`hash_algos()`](http://www.php.net/manual/en/function.hash-algos.php) to find a list of available algorithms.

### p2br

[](#p2br)

```
// Example
{{ 'This is a text.This should be another paragraph.' | p2br }}

// Output
"This is a text.This should be another paragraph."

```

### paragraphs\_slice(offset, length)

[](#paragraphs_sliceoffset-length)

- `offset`: Number of paragraphs to offset.
    - Optional.
    - Default is 0.
- `length`: Number of paragraphs to return.
    - Optional.
    - Default is **null**.

This filter works like [array\_slice](http://www.php.net/manual/en/function.array-slice.php).

#### Examples

[](#examples)

Without parameters, it will return an array containing all the paragraphs:

```
// Example
{{ 'First paragraph.Second paragraph.Third paragraph' | paragraphs_slice }}

// Output
["First paragraph.", "Second paragraph.", "Third paragraph"]

```

Return an array containing the first two paragraphs:

```
// Example
{{ 'First paragraph.Second paragraph.Third paragraph' | paragraphs_slice(0, 2) }}

// Output
["First paragraph.", "Second paragraph."]

```

Return an array containing only the second paragraph:

```
// Example
{{ 'First paragraph.Second paragraph.Third paragraph' | paragraphs_slice(1, 1) }}

// Output
["Second paragraph."]

```

Return an array containing the last paragraph:

```
// Example
{{ 'First paragraph.Second paragraph.Third paragraph' | paragraphs_slice(0, -1) }}

// Output
["Third paragraph"]

```

Twig allows to chain filters, so you can join the resulting array using [join](http://twig.sensiolabs.org/doc/filters/join.html)...

```
// Example
{{ 'First paragraph.Second paragraph.Third paragraph' | paragraphs_slice(0, 2) | join }}

// Output
"First paragraph.Second paragraph."

```

...and to make Twig process HTML, chain the [raw](http://twig.sensiolabs.org/doc/filters/raw.html) filter:

```
{{ 'First paragraph.Second paragraph.Third paragraph' | paragraphs_slice(0, 2) | join | raw }}

```

That way it is possible to truncate a text at a paragraph level.

### regex\_replace(pattern, replacement, limit)

[](#regex_replacepattern-replacement-limit)

- `pattern`: Pattern to search for. It can be either a string or an array with strings.
- `replacement`: String or array with strings to replace.
- `limit`: Maximum possible replacements for each pattern in each subject string.
    - Optional.
    - Default is **-1 (no limit)**.

```
// Example
{{ '1) This is the first title2) This is the second title42)   Another title' | regex_replace('#\\d+\\)\\s*#', '') }}

// Output
"This is the first titleThis is the second titleAnother title"

```

### repeat(num)

[](#repeatnum)

- `num`: Number of times to repeat the string.

```
// Example
{{ '-=' | repeat(10) }}

// Output
"-=-=-=-=-=-=-=-=-=-="

```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

3984d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/be271db0bc92d30320293f761662e84dec7d3ef5ae3f7b037cba1c2f1c2ac8d5?d=identicon)[Falc](/maintainers/Falc)

---

Top Contributors

[![Falc](https://avatars.githubusercontent.com/u/246051?v=4)](https://github.com/Falc "Falc (21 commits)")

---

Tags

twigtext

### Embed Badge

![Health badge](/badges/falc-twig-text-extension/health.svg)

```
[![Health](https://phpackages.com/badges/falc-twig-text-extension/health.svg)](https://phpackages.com/packages/falc-twig-text-extension)
```

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91292.0M313](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36663.2M218](/packages/twig-intl-extra)[rcrowe/twigbridge

Adds the power of Twig to Laravel

9105.9M50](/packages/rcrowe-twigbridge)[twig/string-extra

A Twig extension for Symfony String

21946.0M132](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

23018.5M53](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21814.8M161](/packages/symfony-ux-twig-component)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
