Integrating php-dfa-sensitive for Sensitive Word Detection in PHP Applications

This guide explains how to install the php-dfa-sensitive library via Composer, create a SensitiveWords service class with helper methods, configure custom word dictionaries, and use the provided static functions to detect, replace, or mark sensitive words in PHP projects.

php Courses
php Courses
php Courses
Integrating php-dfa-sensitive for Sensitive Word Detection in PHP Applications

When a project requires detecting sensitive words in user signatures or replies, the php-dfa-sensitive extension offers a convenient solution.

Install the package with Composer: composer require lustre/php-dfa-sensitive Create a SensitiveWords.php file under the app/Services directory and define the service class, which wraps the DfaFilter\SensitiveHelper singleton and provides static methods such as getInstance, isLegal, replace, mark, and getBadWord for various operations.

<?php
namespace App\Services;
use DfaFilter\SensitiveHelper;
class SensitiveWords {
    protected static $handle = null;
    private function __construct() {}
    private function __clone() {}
    /**
     * Get instance
     */
    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;
    }
    /**
     * Check if content contains sensitive words
     */
    public static function isLegal($content) {
        return self::getInstance()->islegal($content);
    }
    /**
     * Replace sensitive words
     */
    public static function replace($content, $replace_char = '', $repeat = false, $match_type = 1) {
        return self::getInstance()->replace($content, $replace_char, $repeat, $match_type);
    }
    /**
     * Mark sensitive words
     */
    public static function mark($content, $start_tag, $end_tag, $match_type = 1) {
        return self::getInstance()->mark($content, $start_tag, $end_tag, $match_type);
    }
    /**
     * Get sensitive words from text
     */
    public static function getBadWord($content, $match_type = 1, $word_num = 0) {
        return self::getInstance()->getBadWord($content, $match_type, $word_num);
    }
}

After setting up the service, you can use the static methods directly in your code. For example, to retrieve any detected sensitive words:

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

Place your custom word lists (e.g., bk.txt, fd.txt, etc.) in a storage/dict directory; the library will load them automatically.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendsecurityPHPComposerDFAsensitive words
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

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.