xp-lang / php-pattern-operator
Pattern operator for PHP
v0.2.0
2024-03-24 13:08 UTC
Requires
- php: >=7.4.0
- xp-framework/compiler: ^9.0 | ^8.0 | ^7.0
- xp-framework/core: ^12.0 | ^11.0 | ^10.0
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.0
README
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
- xp-framework/compiler#130 Motivation
- https://www.perltutorial.org/perl-regular-expression/
- https://stackoverflow.com/questions/19441521/bash-regex-operator
- https://puppet.com/docs/puppet/latest/lang_expressions.html (
=~
and!~
) - https://github.com/kasei/SwiftRegex and https://github.com/johnno1962/SwiftRegex
- https://externals.io/message/4825 (PHP internals discussion, 2003)