Skip to content

Latest commit

 

History

History
executable file
·
60 lines (47 loc) · 2.23 KB

File metadata and controls

executable file
·
60 lines (47 loc) · 2.23 KB

Pattern operator for PHP

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.4+ Supports PHP 8.0+ Latest Stable Version

Plugin for the XP Compiler which adds an ~ infix operator to the PHP language.

Examples

The pattern operator treats the following expression as a regular expression and returns the matches it generates:

if ($greeting ~ '/^H[ea]llo/') {
  // Matches!
}

if ($matches= $greeting ~ '/^H[ea]llo/') {
  // Work with $matches
}

The pattern is passed to preg_match, so the above is equivalent of writing the following:

if (preg_match($greeting, '/^H[ea]llo/')) {
  // Matches!
}

if (preg_match($greeting, '/^H[ea]llo/', $matches)) {
  // Work with $matches
}

Installation

After installing the XP Compiler into your project, also include this plugin.

$ composer require xp-framework/compiler
# ...

$ composer require xp-lang/php-pattern-operator
# ...

No further action is required.

See also