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(2y ago)03.9k1MITPHPCI failing

Since Jan 24Pushed 2y 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 3w ago

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

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

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

754d 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

[verbb/formie

The most user-friendly forms plugin for Craft.

102393.6k70](/packages/verbb-formie)[wbrowar/guide

A CMS Guide for Craft CMS.

6154.7k1](/packages/wbrowar-guide)[verbb/vizy

A flexible visual editor field for Craft.

4250.4k](/packages/verbb-vizy)[verbb/hyper

A user-friendly links field for Craft.

24147.8k12](/packages/verbb-hyper)[verbb/footnotes

Adds a footnotes feature to CKEditor fields and Twig templates.

213.6k](/packages/verbb-footnotes)[verbb/events

A full-featured plugin for event management and ticketing.

2311.9k](/packages/verbb-events)

PHPackages © 2026

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