Backend Development 3 min read

Using the php-dfa-sensitive Library for Sensitive Word Detection in PHP Projects

This article explains how to install the php-dfa-sensitive Composer package, create a SensitiveWords service in a Laravel‑style PHP application, and use its static methods to detect, replace, mark, or retrieve illegal words from user‑generated content.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using the php-dfa-sensitive Library for Sensitive Word Detection in PHP Projects

The article introduces a PHP extension called php-dfa-sensitive , which provides DFA‑based sensitive‑word filtering capabilities for applications that need to validate user signatures or messages.

Installation is performed via Composer:

composer require lustre/php-dfa-sensitive

After installing, a new service class SensitiveWords should be placed under the app/Services directory. The class encapsulates a singleton instance of DfaFilter\SensitiveHelper and loads default dictionaries located in storage/dict (e.g., bk.txt , fd.txt , ms.txt , etc.).

<?php
namespace App\Services;
use DfaFilter\SensitiveHelper;

class SensitiveWords {
    protected static $handle = null;
    private function __construct() {}
    private function __clone() {}
    public static function getInstance($word_path = []) {
        if (!self::$handle) {
            $default_path = [
                storage_path('dict/bk.txt'),
                storage_path('dict/fd.txt'),
                storage_path('dict/ms.txt'),
                storage_path('dict/qt.txt'),
                storage_path('dict/sq.txt'),
                storage_path('dict/tf.txt'),
            ];
            $paths = array_merge($default_path, $word_path);
            self::$handle = SensitiveHelper::init();
            if (!empty($paths)) {
                foreach ($paths as $path) {
                    self::$handle->setTreeByFile($path);
                }
            }
        }
        return self::$handle;
    }
    public static function isLegal($content) {
        return self::getInstance()->islegal($content);
    }
    public static function replace($content, $replace_char = '', $repeat = false, $match_type = 1) {
        return self::getInstance()->replace($content, $replace_char, $repeat, $match_type);
    }
    public static function mark($content, $start_tag, $end_tag, $match_type = 1) {
        return self::getInstance()->mark($content, $start_tag, $end_tag, $match_type);
    }
    public static function getBadWord($content, $match_type = 1, $word_num = 0) {
        return self::getInstance()->getBadWord($content, $match_type, $word_num);
    }
}

In application code you can now call the static methods, for example to retrieve any illegal words:

$bad_word = SensitiveWords::getBadWord($content);
if (!empty($bad_word)) {
    throw new \Exception('包含敏感词:' . current($bad_word));
}

Place your custom word lists in storage/dict and add them to the array passed to getInstance if needed. The article concludes with a reminder that the content is sourced from the internet.

backendphpComposersensitive-word-detectionDFAText Filtering
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.