PHPackages                             magnusjt/regtemplate\_php - 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. magnusjt/regtemplate\_php

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

magnusjt/regtemplate\_php
=========================

Match a template and extract the variables

1.0.0(10y ago)061MITPHP

Since Jul 13Pushed 10y ago1 watchersCompare

[ Source](https://github.com/magnusjt/regtemplate_php)[ Packagist](https://packagist.org/packages/magnusjt/regtemplate_php)[ RSS](/packages/magnusjt-regtemplate-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

regtemplate\_php
================

[](#regtemplate_php)

PHP class for parsing a template into a regex, used for matching into name-value pairs. This is useful when you want to create a regex from a large amount of text without having to fiddle with all the details of the regex (such as escaping text).

Ex:

```
$template->parse_template('Some number: {{ number|digits }}');
$name_value = $template->match('Some number: 5678');

/*
Should result in:

$name_value = [
    'number' => 5678
];
*/

```

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

[](#installation)

Install with composer:

```
composer require magnusjt/regtemplate_php

```

Usage
-----

[](#usage)

See also examples in the examples folder.

### Basic usage example:

[](#basic-usage-example)

```
$template = \RegTemplate\RegTemplate('some/dir');

# Parse a template from a file inside the base directory
$template->parse_template_from_file('file_inside_some_dir.txt');

# Or parse from a string:
# $template->parse_template('Some number: {{ number|digits }}');

# Match an output string against the template. Name=>value array returned.
$name_value = $template->match('Some number: 64');

# Match all. If the regex matches several times, return a list of list of name=>value arrays:
$match_list = $template->match_all('Some number: 64, Some number: 65');
```

### Example template:

[](#example-template)

```
Displaying numbers from index {{ index_name|word }}
Number of interesting events: {{ num_events|digits }}
Number of pages: {{ num_pages|digits }}
Status: {{ on_or_off|reg="(?:on|off)" }}

```

### Adding custom rules:

[](#adding-custom-rules)

A rule is a name and a regex to match it. If the regex has capturing groups, ensure that they are non-capturing like this: (?:).

```
$template->set_rule('digits', '\d+');
$template->set_rule('on_or_off', '(?:on|off)');

# Default rule if no rule is given in the template
$template->set_default_rule('\S+');
```

### Inline regexes:

[](#inline-regexes)

You can use inline regexes instead of rules in your template. Remember to escape any double quotes with backslash.

```
$template->parse_template('{{ number|reg="\d\d\d" }}
```

### Ignoring whitespace (on by default):

[](#ignoring-whitespace-on-by-default)

Ignore any excess whitespace by replacing all whitespace with a single space. You can turn this feature on/off like this:

```
$template->set_ignore_whitespace(true);
```

### Setting custom variable tokens (defaults to '{{' and '}}'):

[](#setting-custom-variable-tokens-defaults-to--and-)

If the string you want to match contains the default variable tokens, you can change the tokens like this:

```
$template->set_variable_tokens('{{', '}}');
```

### Ignoring a variable (default ignore variable name 'any'):

[](#ignoring-a-variable-default-ignore-variable-name-any)

Any variable with the name 'any' is skipped. It is matched, but not returned. You can change the name to ignore like this:

```
$template->set_ignore_var_name('any');
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 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

Unknown

Total

1

Last Release

3961d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0078388acec827c3aa742b3d4c464570b8957d2307de9278db3f9c077b78f751?d=identicon)[magnusjt](/maintainers/magnusjt)

---

Top Contributors

[![magnusjt](https://avatars.githubusercontent.com/u/5463727?v=4)](https://github.com/magnusjt "magnusjt (14 commits)")

---

Tags

templateregexreverse

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/magnusjt-regtemplate-php/health.svg)

```
[![Health](https://phpackages.com/badges/magnusjt-regtemplate-php/health.svg)](https://phpackages.com/packages/magnusjt-regtemplate-php)
```

###  Alternatives

[phpoffice/phpword

PHPWord - A pure PHP library for reading and writing word processing documents (OOXML, ODF, RTF, HTML, PDF)

7.5k34.7M186](/packages/phpoffice-phpword)[rize/uri-template

PHP URI Template (RFC 6570) supports both expansion &amp; extraction

420137.3M46](/packages/rize-uri-template)[mopa/bootstrap-sandbox-bundle

Seperate live docs from code

256.8k](/packages/mopa-bootstrap-sandbox-bundle)[larablocks/pigeon

A more flexible email message builder for Laravel 5 including chained methods, reusable message configurations, and message layout and template view management.

143.7k](/packages/larablocks-pigeon)

PHPackages © 2026

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