PHPackages                             emandiev/regex-replace-twig-filter - 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. emandiev/regex-replace-twig-filter

ActiveCraft-plugin[Templating &amp; Views](/categories/templating)

emandiev/regex-replace-twig-filter
==================================

Adds PHP's preg\_replace function as a Twig filter.

2.0.0(1y ago)03.9k1MITPHPCI failing

Since Jan 24Pushed 1y ago1 watchersCompare

[ Source](https://github.com/emandiev/regex-replace-twig-filter)[ Packagist](https://packagist.org/packages/emandiev/regex-replace-twig-filter)[ RSS](/packages/emandiev-regex-replace-twig-filter/feed)WikiDiscussions master Synced yesterday

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

Regex Replace Twig Filter plugin for Craft CMS 3.x/4.x
======================================================

[](#regex-replace-twig-filter-plugin-for-craft-cms-3x4x)

Adds PHP's preg\_replace function as a Twig filter.

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

[](#requirements)

This plugin requires Craft CMS 3.x or 4.x

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

[](#installation)

To install the plugin, follow these instructions.

1. Open your terminal and go to your Craft project:

```
cd /path/to/project

```

2. Then tell Composer to load the plugin:

```
composer require emandiev/regex-replace-twig-filter

```

3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Regex Replace Twig Filter.

Usage
-----

[](#usage)

```
{{ entry.heading|preg_replace('~pattern~', 'replacement') }}

```

Any backslashes in the regular expression **must** to be double-escaped `\\` to work properly.

How is this different from Craft's [replace filter](https://docs.craftcms.com/v3/dev/filters.html#replace)?
-----------------------------------------------------------------------------------------------------------

[](#how-is-this-different-from-crafts-replace-filter)

In short, the **preg\_replace** filter offers a more direct call to PHP's `preg_replace` function.

Craft's filter allows for both normal string and regex replacements but slightly limits the regex replacement functionality.

### Example 1

[](#example-1)

**preg\_replace** allows you to use any delimiter, while **replace** works only with forward slashes `/`.

```
{{ entry.heading|preg_replace('~pattern~', 'replacement') }}

```

### Example 2

[](#example-2)

The **replace** filter performs `str_replace` when an array is passed and always ignores the `$limit` parameter.

**preg\_replace** binds all it's parameters to [PHP's function](https://www.php.net/manual/en/function.preg-replace.php).

This allows you to pass arrays for the `$pattern` and `$replacement` parameters, and an integer for the `$limit` parameter.

```
{{ entry.heading|preg_replace(['~pattern1~', '~pattern2~'], ['replacement1', 'replacement2'], 3) }}

```

Credits
-------

[](#credits)

Brought to you by [Danail Emandiev](https://emandiev.com/)

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

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 ~1599 days

Total

2

Last Release

701d ago

Major Versions

1.0.1 → 2.0.02024-06-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/1b52b16b0d8dccc46a393577a2dd45fe0089bf16a243de2669c9b69ad126eaf7?d=identicon)[emandiev](/maintainers/emandiev)

---

Top Contributors

[![emandiev](https://avatars.githubusercontent.com/u/15157695?v=4)](https://github.com/emandiev "emandiev (10 commits)")

---

Tags

cmsCraftcraftcmscraft-pluginregex replace twig filter

### Embed Badge

![Health badge](/badges/emandiev-regex-replace-twig-filter/health.svg)

```
[![Health](https://phpackages.com/badges/emandiev-regex-replace-twig-filter/health.svg)](https://phpackages.com/packages/emandiev-regex-replace-twig-filter)
```

###  Alternatives

[lewisjenkins/craft-dynamic-fields

Populate Craft fields with dynamic data using the power of Twig.

14667.0k](/packages/lewisjenkins-craft-dynamic-fields)[nystudio107/craft-minify

A simple plugin that allows you to minify blocks of HTML, CSS, and JS inline in Craft CMS templates.

37461.2k29](/packages/nystudio107-craft-minify)[jalendport/craft-preparse

A fieldtype that parses Twig when an element is saved and saves the result as plain text.

1086.4k](/packages/jalendport-craft-preparse)[craftpulse/craft-colour-swatches

Let clients choose from a predefined set of colours and utilise associated colour codes and class names in your templates.

3530.8k2](/packages/craftpulse-craft-colour-swatches)[verbb/footnotes

Adds a footnotes feature to CKEditor fields and Twig templates.

213.3k](/packages/verbb-footnotes)

PHPackages © 2026

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