chattervast/expressive

Adds PHP PCRE preg functions as Twig filters.

dev-main 2021-10-21 01:44 UTC

This package is auto-updated.

Last update: 2024-05-21 18:04:08 UTC


README

Adds PHP preg functions as Twig filters.

Screenshot

Requirements

This plugin requires Craft CMS 3.0.0-beta.23 or later.

Installation

To install the plugin, search for Expressive in the Plugin Store or 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 Chattervast/expressive
    
  3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Expressive.

Expressive Overview

Adds PHP preg functions as Twig filters. preg_filter

preg_grep

preg_last_error

preg_match

preg_match_first

preg_match_all

preg_quote

preg_replace

preg_replace_callback

preg_replace_callback_array

preg_split

Using Expressive

Twig will remove backslashes so double backslash your regex patterns.

Filter

{{ entry.field|preg_filter(pattern, replacement, limit) }}

Grep

{{ entry.field|preg_grep(pattern) }}

Last Error

This filter returns the last error given on any preg function. The preg function to test with must be specified and the appropriate parameters needed for that function to work.

Testing preg_filter

{{ entry.field|preg_last_error('filter', pattern, replacement, limit) }}

Testing preg_grep

{{ entry.field|preg_last_error('grep', pattern) }}

Testing preg_match

{{ entry.field|preg_last_error('match', pattern) }}

Testing preg_match_first

{{ entry.field|preg_last_error('matchfirst', pattern) }}

Testing preg_match_all

{{ entry.field|preg_last_error('matchall', pattern) }}

Testing preg_quote

{{ entry.field|preg_last_error('quote', delimiter) }}

Testing preg_replace

{{ entry.field|preg_last_error('replace', pattern, replacement, limit) }}

Testing preg_replace_callback

{{ entry.field|preg_last_error('callback', pattern, callback) }}

Testing preg_replace_callback_array

{{ entry.field|preg_last_error('callbackarray', pattern, limit, count) }}

Testing preg_split

{{ entry.field|preg_last_error('split', pattern) }}

Match

{{ entry.field|preg_match(pattern) }}

Match First

{{ entry.field|preg_match_first(pattern) }}

Match All

{{ entry.field|preg_match_all(pattern) }}

Quote

{{ entry.field|preg_quote(delimiter) }}

Replace

{{ entry.field|preg_replace(pattern, replacement, limit) }} Example: Replace email addresses with mailto links: {{ entry.field|preg_replace('/(\S+@\S+\.\S+)/', '<a href=mailto:"$1">$1</a>')|raw }} Adding the |raw filter will render the HTML.

Replace Callback

{{ entry.field|preg_replace_callback(pattern, callback) }}

Replace Callback Array

{{ entry.field|preg_replace_callback_array(pattern, limit, count) }}

Split

{{ entry.field|preg_split(pattern) }}

Brought to you by Chattervast