piyo2/str

Normalizing/filtering string

v1.0.0 2022-10-27 04:05 UTC

This package is auto-updated.

Last update: 2024-11-27 08:22:33 UTC


README

Normalizing/filtering string.

Features

  • Remove control characters (except for tabs or newlines)
  • Normalize Unicode composition sequences
  • But keep CJK compatibility ideographs
  • Block invalid/overlong UTF-8 sequences
  • Transform functions:
    • Uppercase <=> lowercase
    • Zenkaku <=> Hankaku
    • Hiragana <=> Katakana (including ゕ ゖ ゝ ゞ)

Requirements

  • PHP ≥ 7.1.0
  • Intl PHP Extension
  • Mbstring PHP Extension

Example

$filter = (new Str())->trim()
	->noNewlines()
	->hankakuDigits()
	->hiragana();

$filter->applyTo(" A03 \nアイス\n");
// => "A03 アイス"

Applying user-defined filter:

(new Str())->trim()
	->hankaku()
	->fn(fn ($s) => '#' . $s)
	->applyTo(' 123');
// => "#123"

Retain control characters:

(new Str())->applyTo("\0\n");
// => "\n"

(new Str(true))->applyTo("\0\n");
// => "\0\n"