Mask

StrMask is a Solital class to make masks for a string

In your PHP application, use the static method apply of Mask:

use Solital\Core\Resource\Str\StrMask;

$output = StrMask::apply($inputValue, $maskExpression, $config);

Arguments

  • $inputValue: string - The input value to apply mask.
  • $maskExpression: string - The mask expression for $output.
  • $config: array - The configuration for operation

Patterns

The patterns are used to filter $inputValue:

Code Meaning
**0** digits (like 0 to 9 numbers)
**9** digits (like 0 to 9 numbers), but optional
**A** letters (uppercase or lowercase) and digits
**S** only letters (uppercase or lowercase)

Special chars

Special chars are used in mask expressions to format output:

  • /
  • (
  • )
  • .
  • :
  • -
  • space
  • +
  • ,
  • @

Thousand separator

You can format a number in thousand separator and control precision.

The mask keys are:

  • separator: Input 1234.56 is ouputed as 1 234.56
  • dot_separator: Input 1234,56 is ouputed as 1.234,56
  • comma_separator: Input 1234.56 is ouputed as 1,234.56

To manage precision, keys shall be suffixed by .{Number}.

Example:

  • separator.1: Input 1234.56743 is ouputed as 1 234.5
  • dot_separator.4: Input 1234,56743 is ouputed as 1.234,5674
  • comma_separator.2: Input 1234.56743 is ouputed as 1,234.56

Time validation

You can format a time according limit:

Mask Meaning
**H** Input value shall be inside 0 and 2.
**h** Input value shall be inside 0 and 3.
**m** Input value shall be inside 0 and 4.
**s** Input value shall be inside 0 and 5.

Percent validation

You can format a value from $inputValue as a percent and manage the precision. Use the key percent to have a extract value from $inputValue within 0 to 100. Suffix the key with .{Number} to manage precision (percent.2).

Example:

$output = StrMask::apply("99.4125", "percent.2");

// $output contains: 99.41

Prefix and suffix

You have possibility to set suffix and prefix in output:

$output = StrMask::apply("0102030405", "00 00 00 00 00", [
  "prefix" => "My phone is ",
  "suffix" => "!"
]);

// $output contains: My phone is 01 02 03 04 05!

What to see next?


Built with MkDocs.