Cut

Cut - Limit string length.

Cut only works with string, any other type is ignored.

Basic usage

use Jawira\Sanitizer\Filters as Sanitizer;

class Article {
  #[Sanitizer\Cut(length: 255)]
  public string $title;
}

Parameters

int length:
If desired length is positive, the resulting string will start from the beginning of the string.
If length is negative, the resulting string will start from the ending of the string.
bool useBytes (optional):
When false, length will represent characters.
If true, length will be in bytes.
Default value is false.

Examples

Limit the length of string to 5 characters.

use Jawira\Sanitizer\Filters as Sanitizer;

class Project {
  #[Sanitizer\Cut(length: 5)]
  public string $name;
}
"CPU-486"   → "CPU-4"
"しょうぼうし" → "しょうぼう"

Limit the length of string to the last 3 characters.

use Jawira\Sanitizer\Filters as Sanitizer;

class Project {
  #[Sanitizer\Cut(length: -3)]
  public string $name;
}
"CPU-486"   → "486"
"しょうぼうし" → "ぼうし"

The string must be 3 bytes in size.

use Jawira\Sanitizer\Filters as Sanitizer;

class Project {
  #[Sanitizer\Cut(length: 3, useBytes: true)]
  public string $name;
}
"CPU-486"   → "CPU"
"しょうぼうし" → "し"

See also

  • Trim - Strip whitespace (or other characters) from the beginning and end of a string.