将字符串掩码转换为正则表达式模式

1.0.1 2019-08-31 08:39 UTC

This package is auto-updated.

Last update: 2024-08-29 05:07:06 UTC


README

standard-readme compliant

将字符串掩码转换为正则表达式模式

目录

安装

使用 composerxmask 添加到您的项目的依赖项中。

$ composer require michaelbiberich/xmask ^1

测试

可以使用 PHPUnit 运行测试,请参阅 phpunit.xml.dist

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

用法

<?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)

维护者

@michaelbiberich.

贡献

请随意深入!创建一个问题 或提交PR。

许可证

MIT © Michael Biberich