hartpableo/php-censoring-tool 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

  1. Install development dependencies:
    composer install
  2. 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
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固