Pad
Pad - Pad a string to a certain length with another string.
Pad only works with string
, any other type is ignored.
Basic usage
use Jawira\Sanitizer\Filters as Sanitizer;
class Report {
#[Sanitizer\Pad(length: 10)]
public string $category;
}
Parameters
- int
length
: - Length of resulting string once pad is applied.
- string
padString
(optional): -
String to use as pad.
The default value is "space" character. - string
side
(optional): -
Use
right
to apply pad at the end of string, this is the default value.
Useleft
to apply pad at the beginning of string.
Useboth
to apply pad at the beginning and the end of string.
Examples
Add leading zeros when string has less than three characters:
use Jawira\Sanitizer\Filters as Sanitizer;
class Classroom {
#[Sanitizer\Pad(length: 3, padString: '0', side: 'left')]
public string $number;
}
"1" → "001"
"50" → "050"
"312" → "312"
Create 30 characters width ascii art header:
use Jawira\Sanitizer\Filters as Sanitizer;
class AsciiArt {
#[Sanitizer\Pad(length: 30, padString: '-+-', side: 'both')]
public string $title;
}
"CREDITS" → "-+--+--+--+CREDITS-+--+--+--+-"
"DOCUMENTATION" → "-+--+--+DOCUMENTATION-+--+--+-"
"AUTHOR" → "-+--+--+--+-AUTHOR-+--+--+--+-"
Right padding of 30 characters with asterisk symbol:
use Jawira\Sanitizer\Filters as Sanitizer;
class Paycheck {
#[Sanitizer\Pad(length: 30, padString: '*')]
public string $writtenAmount;
}
"four thousand" → "four thousand*****************"
"one thousand five hundred" → "one thousand five hundred*****"
See also
Trim - Strip whitespace (or other characters) from the beginning and end of a string.