类似于 Vanilla Masker 的字符串遮罩工具,但使用 PHP 实现

0.1.1 2024-06-13 15:58 UTC

This package is auto-updated.

Last update: 2024-09-13 16:29:43 UTC


README

这个库可以帮助您使用简单的遮罩格式化文本或数字。

安装

为了安装,您需要使用 8.1、8.2 或 8.3 版本的 PHP。

composer require wallacemaxters/masker

基本用法

use WallaceMaxters\Masker\Masker;

$masker = new Masker();

$masker->mask('31995451199', '(00) 00000-0000'); // '(31) 99545-1199'
// or 
$masker('31995451199', '(00) 00000-0000'); // '(31) 99545-1199'

您可以使用任何字符来格式化文本。

$masker->mask('ABC12345', '[AAA]_(00000)'); // '[ABC]_(12345)'

如果需要,您可以返回 string 的未遮罩值。

$masker->unmask('[ABC]_(12345)', '[AAA]_(00000)'); // 'ABC12345'

dynamic 方法允许传递多个遮罩作为参数。这使得您的字符串可以根据大小进行格式化。

$cpf_or_cnpj = ['000.000.000-00', '00.000.000/0000-00'];

$masker->dynamic('45522248327', $cpf_or_cnpj); // '455.222.483-27'

$masker->dynamic('68544172000160', $cpf_or_cnpj); // '68.544.172/0001-60'