hartpableo/php-censoring-tool
Composer 安装命令:
composer require hartpableo/php-censoring-tool
包简介
A simple PHP censoring tool to hide emails and links in a text.
README 文档
README
A lightweight, flexible PHP library to censor email addresses and hyperlinks in block text. Ideal for cleaning user-generated content, masking database outputs, or protecting private information before displaying it publicly.
Features
- Email Censoring: Matches and obscures standard email addresses.
- Link Censoring: Identifies HTML anchor tags (
<a>...</a>) and obscures either the anchor text or the full tag. - Flexible Masking: Decoupled design allows you to define custom replacement formats (e.g., HTML, markdown, custom text like
[REDACTED], or simple asterisks). - Zero Production Dependencies: Light codebase that runs out-of-the-box on PHP 8.0 or newer.
Installation
Install the package via Composer:
composer require hartpableo/php-censoring-tool
Usage
1. Basic Usage (Default HTML Output)
By default, censored links and emails are replaced with standard HTML tags styled with the class .censored-text and carrying a notice.
use HartPableo\PhpCensoringTool\Censor; $text = "Contact support@example.com or visit <a href='https://example.com'>Example Site</a>."; $censor = new Censor($text, "Please upgrade your subscription to view contact details."); $output = $censor->getCensoredText(); echo $output; // Contact <strong class="censored-text" data-notice="Please upgrade your subscription to view contact details.">*******************</strong> or visit <strong class="censored-text" data-notice="Please upgrade your subscription to view contact details.">************</strong>.
2. Advanced Usage (Custom Formatting Callback)
If you need a plain text output, Markdown formatting, or custom redacted strings, you can pass a custom formatter as the third argument to the constructor:
use HartPableo\PhpCensoringTool\Censor; $text = "Send your CV to jobs@example.com."; // Censor using a simple '[REDACTED]' string: $censor = new Censor($text, 'Redacted', function(string $string) { return '[REDACTED]'; }); echo $censor->getCensoredText(); // Send your CV to [REDACTED].
Another example using custom asterisk replacement:
$censor = new Censor($text, '', function(string $string) { return str_repeat('*', strlen($string)); });
Testing
Automated Unit Tests
The library includes a PHPUnit test suite. To run the automated unit tests:
- Install development dependencies:
composer install
- Run the test suite:
vendor/bin/phpunit
Manual Sample Run
You can also run the quick demonstration script:
php tests/index.php
License
This project is open-source software licensed under the MIT License.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-20