Convert string masks to regular expression patterns

1.0.1 2019-08-31 08:39 UTC

This package is auto-updated.

Last update: 2024-10-29 05:40:20 UTC


README

standard-readme compliant

Convert string masks to regular expression patterns

Table of Contents

Install

Add xmask as a dependency to your project with composer.

$ composer require michaelbiberich/xmask ^1

Tests

Tests can be run using PHPUnit, see phpunit.xml.dist.

$ phpunit --configuration ./phpunit.xml.dist

Usage

<?php

declare(strict_types=1);

require_once 'vendor/autoload.php';

use MichaelBiberich\Xmask\Xmask;

$xmask = new Xmask('xxx456xxx', [
    'x' => '0-9',
]);

$pattern = $xmask->pattern();
// => string(67) "/^([0-9])([0-9])([0-9])456([0-9])([0-9])([0-9])$/"

preg_match($pattern, '123456789'); // => int(1)
preg_match($pattern, '123000789'); // => int(0)
preg_match($pattern, 'abc456789'); // => int(0)

// Examples: Value added tax identification numbers (VATINs)

$austriaVatinXmask = new Xmask('ATUxxxxxxxxx', [
    'x' => '0-9',
]);

$austriaVatinPattern = $austriaVatinXmask->pattern();

preg_match($austriaVatinPattern, 'ATU123456789'); // => int(1)
preg_match($austriaVatinPattern, 'ATU12345678'); // => int(0)

$cyprusVatinXmask = new Xmask('CYxxxxxxxxX', [
    'x' => '0-9',
    'X' => 'A-Z',
]);

$cyprusVatinPattern = $cyprusVatinXmask->pattern();
// => string(96) "/^CY([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([A-Z])$/"

preg_match($cyprusVatinPattern, 'CY12345678A'); // => int(1)
preg_match($cyprusVatinPattern, 'CY123456789'); // => int(0)

Maintainers

@michaelbiberich.

Contributing

Feel free to dive in! Open an issue or submit PRs.

License

MIT © Michael Biberich