accentinteractive/disallowlister

测试字符串是否在禁止列表中。适用于用户表单等。

v0.4.0 2021-08-27 16:08 UTC

This package is auto-updated.

Last update: 2024-08-27 22:46:39 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

这个小包测试字符串是否在禁止列表中。

如果您正在寻找特定于Laravel的实现,请查看https://github.com/accentinteractive/laravel-disallowlister

isDisallowed()方法可以使用通配符,如*。

在内部,accentinteractive/disallowtester使用fnmatch(),因此您可以使用与该php函数相同的通配符(globbing通配符模式)。

  • *sex*禁止sexsexuality和bisexual。

  • cycle*禁止cycle和cycles,但不禁止bicycle。

  • m[o,u]m禁止mom和mum,但允许mam。

  • m?n禁止man和men,但允许moon。

  • 安装

  • 示例

  • 配置设置

安装

您可以通过composer安装此包

composer require accentinteractive/disallowlister

用法

设置禁止列表

您可以通过构造函数或其它方法传递禁止列表。

// Pass the disallowlist in the constructor 
$disallowLister = new DisallowLister(['foo']); // ['foo']

// Set the disallowlist in the setter method
$disallowLister->setDisallowList(['bar']); // ['bar']

// Add an item to the disallowlist
$disallowLister->add('baz'); // ['bar', 'baz']

// Add multiple items to the disallowlist
$disallowLister->add(['bat', 'fiz']); // ['bar', 'baz', 'bat', 'fiz']

// Remove an item from the disallowlist
$disallowLister->remove('fiz'); // ['bar', 'baz', 'bat']

// Remove multiple items from the disallowlist
$disallowLister->remove(['baz', 'bat']); // ['bar']

检查数据是否在禁止列表中

## Literal string
$disallowLister = new DisallowLister(['bar', 'foo']);

$disallowLister->isDisallowed('bar'); // Returns true
$disallowLister->isDisallowed('bars'); // Returns false

## Wildcards
// Under the hood, `accentinteractive/disallowtester` 
// uses `fnmatch()`, so you can use the same 
// wildcards as in that php function (the 
// globbing wildcard patterns):
(new DisallowLister(['b?r']))->isDisallowed('bar'); // Returns true
(new DisallowLister(['m[o,u]m']))->isDisallowed('mom'); // Returns true
(new DisallowLister(['*bar*']))->isDisallowed('I like crowbars'); // Returns true

大小写敏感

// By default, matching is not case sensitive
(new DisallowLister(['bar']))->isDisallowed('BAR'); // Returns true

// To set case sensitive matching
(new DisallowLister(['bar']))->caseSensitive(true)->isDisallowed('BAR'); // Returns false

全词检查

// By default the entire string is checked. 
(new DisallowLister())->setDisallowList(['bar'])->isDisallowed('My favorite bar'); // Returns false

// Check word for word.
(new DisallowLister())->setDisallowList(['bar'])->setWordForWord(true)->isDisallowed('My favorite bar'); // Returns true

测试

composer test

变更日志

请参阅CHANGELOG以获取更多关于最近更改的信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何安全相关的问题,请发送电子邮件至joost@accentinteractive.nl,而不是使用问题跟踪器。

鸣谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。