consoletvs/profanity

用于在字符串中屏蔽不良词的PHP库

3.5.0 2024-07-03 20:59 UTC

This package is auto-updated.

Last update: 2024-09-03 21:26:50 UTC


README

Profanity

StyleCI Status Build For PHP Total Downloads Latest Stable Version License

什么是Profanity?

Profanity是一个PHP库,它允许从给定字符串中屏蔽不良词。它目前可以屏蔽超过2.555个不同语言中的不良词。它非常容易使用,可以在大约0.0021秒内过滤一个100字符的字符串!

示例过滤器

此示例是一个简单的字符串,将使用此库进行过滤。

$clean_words = Profanity::blocker('My cool string bitch')->filter()

// My cool string *****

文档

安装

首先,使用composer引入它

composer require consoletvs/profanity

Laravel

要在Laravel中安装它,只需在config/app.php中添加服务提供者和别名即可

服务提供者

ConsoleTVs\Profanity\ProfanityServiceProvider::class,

别名(可选)

'Profanity' => ConsoleTVs\Profanity\Facades\Profanity::class,

用法

PHP

$words = 'My bad word bitch';
$clean_words = \ConsoleTVs\Profanity\Builder::blocker($words)->filter();
// My bad word *****

// Filter words only by English and German bad words.
$clean_words = \ConsoleTVs\Profanity\Builder::blocker($words, languages: ['en', 'de'])->filter();

Laravel

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use ConsoleTVs\Profanity\Facades\Profanity;
// or using the alias:
// use Profanity;

class HomeController extends Controller
{
    /**
     * Simple test function.
     *
     * @return string
     */
    public function test()
    {
        $words = 'My bad word bitch';

        return Profanity::blocker($words)->filter();
        // My bad word *****
    }
}

方法

构造函数

  • blocker($text, $blocker = '****', $languages = [])

    设置文本和要使用的屏蔽词。

    注意:屏蔽词默认为****

    注意:传递语言代码可以减少考虑的不良词列表。

类方法

  • strict(true/false)

    设置严格模式为启用或禁用。严格模式即使不是单词也会替换字符串:assets => (标记为ass) => **** 禁用以确保正常使用。默认为false。

  • strictClean(true/false)

    设置严格清洁模式为启用或禁用。严格清洁模式确保重复第一个屏蔽字符,对于每个不良词字符。如果是,长度为3的不良词(ass)将产生***,而不是屏蔽词(默认为****)。如果屏蔽词为-**-,则上面的示例将产生---作为严格清洁true和-**-作为false。默认为true。

  • clean()

    如果文本没有不良词,则返回true。

  • filter()

    使用给定的屏蔽词过滤文本。

  • badWords()

    返回文本中找到的不良词。

  • text($text)

    设置要过滤的文本。

  • blocker($blocker)

    设置将替换不良词的字符串。

  • dictionary($dictionary)

    设置字典。如果提供字符串,它将被视为路径,并尝试在路径中解码json。如果提供数组,它将设置字典。

    示例字典

    [
        {
            "language": "en",
            "word": "bitch"
        },
        {
            "language": "en",
            "word": "fuck"
        }
    ]

许可证

MIT License

Copyright (c) 2017 Erik Campobadal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.