joetannenbaum / mr-clean
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: 4.1.*
This package is auto-updated.
Last update: 2024-08-30 01:06:48 UTC
README
Mr. Clean 是一个可扩展的 PHP 清理器,允许您轻松清理字符串、数组、对象以及两者之间的任何内容。
目录
安装
使用 composer
{
"require": {
"joetannenbaum/mr-clean": "~0.0"
}
}
基本用法
可以这样启动
require_once 'vendor/autoload.php'; $cleaner = new MrClean\MrClean();
清洁器
清洁器是执行实际工作的类和函数,您可以为清理对象分配任意数量的清洁器。
$scrubbers = [ 'trim', 'stripslashes', 'strip_tags', 'remove_weird_characters', ]; $scrubbed = $cleaner->scrubbers($scrubbers)->scrub('I\'m not that dirty.');
清洁器应始终作为数组传递,并将按照您指定的顺序运行。
可以使用任何单个参数字符串操作函数。要引用一个类,只需将 StudlyCase 转换为 snake_case。在上面的示例中,remove_weird_characters
指的是名为 RemoveWeirdCharacters
的(虚构)类。
前后
为了节省一些打字时间,您可以设置清洁器在每次清理前后都运行
$cleaner->pre(['trim']); $cleaner->post(['htmlentities']); // trim will run before each of these, htmlentities after each $cleaner->scrubbers(['strip_tags'])->scrub('This should be cleaned.') $cleaner->scrubbers(['remove_weird_characters'])->scrub('So should this.')
可以清理的内容
更好的问题是:什么不能?数组、字符串、对象数组、单个对象,您可以尝试,Mr. Clean 很可能能够清理它。所有以下内容都将工作
$scrubbed = $cleaner->scrubbers(['trim'])->scrub('Holy string, Batman.'); $scrubbed = $cleaner->scrubbers(['trim'])->scrub(['Holy', 'array', 'Batman']); $scrubbed = $cleaner->scrubbers(['trim'])->scrub([ ['Holy', 'array', 'of', 'arrays', 'Batman'], ['Holy', 'array', 'of', 'arrays', 'Batman'], ]); $scrubbed = $cleaner->scrubbers(['trim'])->scrub((object) [ 'first_word' => 'Holy', 'second_word' => 'object', 'third_word' => 'Batman', ]); $scrubbed = $cleaner->scrubbers(['trim'])->scrub([ (object) [ 'first_word' => 'Holy', 'second_word' => 'array', 'third_word' => 'of', 'fourth_word' => 'objects', 'fifth_word' => 'Batman', ], (object) [ 'first_word' => 'Holy', 'second_word' => 'array', 'third_word' => 'of', 'fourth_word' => 'objects', 'fifth_word' => 'Batman', ], ]); $scrubbed = $cleaner->scrubbers(['trim'])->scrub([ (object) [ 'first_word' => 'Holy', 'second_word' => 'mixed', 'third_word' => ['bag', 'Batman'], ], ]);
清理特定键
有时您不想在对象的每个键或关联数组中使用相同的清洁器。没问题。只需让 Mr. Clean 知道在哪里应用哪些清洁器,他会处理它
$scrubbers = [ 'first_name' => ['trim'], 'last_name' => ['stripslashes', 'htmlentities'], ]; $data = [ [ 'first_name' => 'Joe ', 'last_name' => 'O\'Donnell', ], [ 'first_name' => ' Harold', 'last_name' => 'Frank & Beans', ], ]; $scrubbed = $cleaner->scrubbers($scrubbers)->scrub($data); /* [ [ 'first_name' => 'Joe', 'last_name' => "O'Donnell", ], [ 'first_name' => 'Harold', 'last_name' => 'Frank & Beans', ], ] */
您还可以指定适用于所有内容的清洁器
$scrubbers = [ 'strip_tags', 'first_name' => ['trim'], 'last_name' => ['stripslashes', 'htmlentities'], 'htmlspecialchars', ];
可用清洁器
Mr. Clean 提供了大量的预构建清洁器供您使用
布尔值
将假文本和被认为是 empty
的任何内容转换为 false
,否则返回 true
。假文本包括(不区分大小写)
- no
- n
- false
$movies_seen = [ 'The Dark Knight' => 'y', 'The Green Lantern' => 'n', 'The Avengers' => 'yes', ]; $scrubbed = $cleaner->scrubbers(['boolean'])->scrub( $movies_seen ); /* [ 'The Dark Knight' => true, 'The Green Lantern' => false, 'The Avengers' => true, ]; */
HTML
删除不在白名单上的标签,移除空内容标签,以及重复的打开或关闭标签。白名单包括
- a
- p
- div
- strong
- em
- b
- i
- br
- ul
- ol
- li
- h1
- h2
- h3
- h4
- h5
- h6
$dirty = '<p><p>Some bad HTML here.</p><hr /><em></em><div>Soon to be cleaner.</div>'; $scrubbed = $cleaner->scrubbers(['html'])->scrub( $dirty ); // <p>Some bad HTML here.</p><div>Soon to be cleaner.</div>
删除 CSS 属性
从所有 HTML 元素中删除 style
、class
和 id
属性。
$dirty = '<p style="font-weight:bold;" id="bold-el" class="boldest">This was once bold.</p>'; $scrubbed = $cleaner->scrubbers(['strip_css_attributes'])->scrub($dirty); // <p>This was once bold.</p>
置为 null
如果修剪后的字符串没有长度,将其置为 null
$dirty = [ 'cool', 'also cool', ' ', ' ', ]; $scrubbed = $cleaner->scrubbers(['nullify'])->scrub($dirty); /* [ 'cool', 'also cool', null, null, ]; */
如果重复则置为 null
如果字符串只是重复的字符(如 '1111111' 或 'aaaaaaaaa')并且长度大于两个,将其置为 null
$dirty = [ '11111111', '22', 'bbbbbbbb', '333334', ]; $scrubbed = $cleaner->scrubbers(['null_if_repeated'])->scrub($dirty); /* [ null, '22', null, '333334', ]; */
删除电话号码
将电话号码简化为只包含有效部分,即数字和字母 'x'(用于扩展)
$dirty = [ '555-555-5555', '(123) 456-7890', '198 765 4321 ext. 888', ]; $scrubbed = $cleaner->scrubbers(['strip_phone_number'])->scrub($dirty); /* [ '5555555555', '1234567890', '1987654321x888', ]; */
扩展
您可以使用 Mr. Clean 注册自定义清洁器。
编写清洁器
首先,编写您的类。您只需扩展 MrClean\Scrubber\BaseScrubber
即可,它遵循 MrClean\Scrubber\ScrubberInterface
。您有一个可用的属性,即 value
。这是您将操作的字符串。
namespace Your\Namespace; use MrClean\Scrubber\BaseScrubber; class YourCustomScrubber extends BaseScrubber { public function scrub() { return str_replace('!', '.', $this->value); } }
就是这样。现在只需将您的清洁器注册到 Mr. Clean 中。
注册清洁器
register
方法将接受一个表示类完整路径的字符串,或一个表示类路径的数组。
$cleaner->register('Your\Namespace\YourCustomScrubber');
现在,去使用它吧
$dirty = [ 'I need to calm down!', 'Me too!', ]; $scrubbed = $cleaner->scrubbers(['your_custom_scrubber'])->scrub($dirty); /* [ 'I need to calm down.', 'Me too.', ] */