Download the PHP package overtrue/laravel-text-guard without Composer
On this page you can find all versions of the php package overtrue/laravel-text-guard. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download overtrue/laravel-text-guard
More information about overtrue/laravel-text-guard
Files in overtrue/laravel-text-guard
Package laravel-text-guard
Short Description TextGuard:可作 Rule 也可单用的字符串清洗/规范化工具
License MIT
Informations about the package laravel-text-guard
Laravel TextGuard
A powerful string sanitization and normalization tool for Laravel that can be used as validation rules or standalone utility.
English Documentation
Documentation
- Pipeline Guide - Complete guide to all pipeline steps
- Pipeline 指南 - 所有 pipeline 步骤的完整指南
Installation
Publish Configuration
Basic Usage
As a Utility
As Validation Rules
Model Auto-Filtering
Use the TextGuardable trait to provide automatic filtering for model fields:
When the model is saved, specified fields are automatically filtered:
Dynamic Field Management
FormRequest Integration
Pipeline Usage
Laravel TextGuard provides 14 built-in pipeline steps for text processing. Each pipeline step can be configured with specific parameters to meet different requirements.
Available Pipeline Steps
Basic Text Processing
trim_whitespace- Remove leading/trailing whitespace (including full-width spaces)collapse_spaces- Collapse multiple consecutive spaces into single spaceremove_control_chars- Remove control characters while preserving newlines and tabsremove_zero_width- Remove zero-width characters (U+200B..200D, U+FEFF)
Unicode Processing
unicode_normalization- Unicode normalization (NFC, NFD, NFKC, NFKD)fullwidth_to_halfwidth- Convert fullwidth characters to halfwidthnormalize_punctuations- Normalize punctuation based on locale (zh/en)
HTML Processing
strip_html- Remove all HTML tagshtml_decode- Decode HTML entitieswhitelist_html- Keep only allowed HTML tags and attributes
Character Filtering
character_whitelist- Keep only allowed character types (with emoji support)collapse_repeated_marks- Limit repeated punctuation marks
Length Control
visible_ratio_guard- Check visible character ratiotruncate_length- Truncate text to maximum length
Pipeline Configuration Examples
Basic Text Cleaning
Username Processing
Rich Text Processing
Nickname with Emoji Support
Pipeline Parameter Types
The system automatically passes configuration to constructors based on type:
- Boolean values (
true/false) → No parameter constructor:new Class() - String values (
'NFKC') → Single parameter constructor:new Class('NFKC') - Array values (
['max' => 100]) → Array parameter constructor:new Class(['max' => 100])
Usage Tips
- Order matters: Pipeline steps are executed in configuration order
- Basic cleanup first: Start with
trim_whitespace,collapse_spaces,remove_control_chars,remove_zero_width - Unicode processing: Use
unicode_normalizationandfullwidth_to_halfwidthfor international text - HTML handling: Use
whitelist_htmlfor rich text,strip_htmlfor plain text - Length control last: Place
visible_ratio_guardandtruncate_lengthat the end
For detailed parameter information and more examples, see the Pipeline Guide.
Preset Configurations
Level-based Presets
safe Preset
Default preset suitable for most normal text input fields. Includes basic text sanitization features.
strict Preset
More restrictive filtering mode with stricter rules:
- No emoji characters allowed
- Converts all punctuation to half-width
- Higher visible character ratio requirement (0.8)
- Shorter text length limit (5000)
Function-specific Presets (Examples)
username Preset
Suitable for username input with stricter normalization.
nickname Preset
Suitable for user nicknames with emoji and Chinese punctuation support.
rich_text Preset
Suitable for rich text content, preserving safe HTML tags.
Extended Features
Register Custom Pipeline Steps
Get Available Steps
Create Custom Pipeline Steps
Constructor Configuration
All Pipeline steps now use constructor configuration, following traditional OOP design:
Configuration Passing Mechanism
The system automatically passes configuration to constructors based on type:
true→ No parameter constructornew Class()'NFKC'→ Single parameter constructornew Class('NFKC')['max' => 100]→ Array parameter constructornew Class(['max' => 100])
Real-world Usage Scenarios
User Registration Form
Article Content Management
Comment System
Search Keyword Processing
Batch Data Processing
Custom Pipeline Steps
Features
- Whitespace handling (trim, collapse spaces)
- Control character removal
- Zero-width character removal
- Unicode normalization
- Full-width to half-width conversion
- Punctuation normalization
- HTML tag processing
- Repeated punctuation collapsing
- Visibility checking
- Length truncation
- Extensible pipeline architecture
- Runtime custom step registration
- Emoji support
- Chinese punctuation support
- Character whitelist filtering
Testing
Code Quality
This project follows strict code quality standards:
License
MIT License
All versions of laravel-text-guard with dependencies
illuminate/support Version ^12.0|^13.0
ext-mbstring Version *
ext-intl Version *