dev-options
dev-options
MIT
The Requires
- php >=5.4.0
The Development Requires
dev-master
9999999-dev
MIT
The Requires
- php >=5.4.0
The Development Requires
0.1.0
0.1.0.0
MIT
The Requires
- php >=5.4.0
The Development Requires
Wallogit.com
2017 © Pedro Peláez
Mr. Clean is an extendible PHP cleaner that allows you to easily clean up strings, arrays, objects, and anything in between., (*2)
Using composer:, (*3)
{
"require": {
"joetannenbaum/mr-clean": "~0.0"
}
}
Fire it up like so:, (*4)
require_once 'vendor/autoload.php'; $cleaner = new MrClean\MrClean();
Scrubbers are the classes and functions that actually do the work, and you can assign as many as you want to clean your object., (*5)
$scrubbers = [
'trim',
'stripslashes',
'strip_tags',
'remove_weird_characters',
];
$scrubbed = $cleaner->scrubbers($scrubbers)->scrub('I\'m not that dirty.');
Scrubbers should always be passed as an array, and will be run in the order that you specify., (*6)
Any single argument string manipulation function can be used. To reference a class, simply convert the StudlyCase to snake_case. In the example above, remove_weird_characters refers to a (fictional) class named RemoveWeirdCharacters., (*7)
To save some typing, you can set scrubbers to run every time before and after each cleaning:, (*8)
$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.')
Better question: what can't? An array of arrays, a string, an array of objects, a single object, you try it, Mr. Clean will probably be able to clean it. All of the following will work:, (*9)
$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'],
],
]);
Sometimes you don't want to use the same scrubbers on every key in an object or associative array. No problem. Just let Mr. Clean know which ones to apply where and he'll take care of it:, (*10)
$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',
],
]
*/
You can also still specify scrubbers that should run for everything:, (*11)
$scrubbers = [
'strip_tags',
'first_name' => ['trim'],
'last_name' => ['stripslashes', 'htmlentities'],
'htmlspecialchars',
];
Mr. Clean comes with a bevy of pre-built scrubbers you can use:, (*12)
Converts falsey text and anything considered empty to false, otherwise returns true. Falsey text includes (not case sensitive):, (*13)
$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,
];
*/
Strips tags not on the whitelist, removes empty content tags, and repeated opening or closing tags. The whitelist includes:, (*14)
$dirty = ' < p>Some bad HTML here., (*15)
<em></em>Soon to be cleaner.'; $scrubbed = $cleaner->scrubbers(['html'])->scrub( $dirty ); //Some bad HTML here., (*16)
Soon to be cleaner.
Strips the style, class, and id attributes off of all HTML elements., (*17)
$dirty = 'This was once bold., (*18)
'; $scrubbed = $cleaner->scrubbers(['strip_css_attributes'])->scrub($dirty); //This was once bold., (*19)
If a trimmed string doesn't have any length, null it out:, (*20)
$dirty = [
'cool',
'also cool',
' ',
' ',
];
$scrubbed = $cleaner->scrubbers(['nullify'])->scrub($dirty);
/*
[
'cool',
'also cool',
null,
null,
];
*/
If a string is just a repeated character ('1111111' or 'aaaaaaaaa') and has a length greater than two, null it out:, (*21)
$dirty = [
'11111111',
'22',
'bbbbbbbb',
'333334',
];
$scrubbed = $cleaner->scrubbers(['null_if_repeated'])->scrub($dirty);
/*
[
null,
'22',
null,
'333334',
];
*/
Strip a phone number down to just the good bits, numbers and the letter 'x' (for extensions):, (*22)
$dirty = [
'555-555-5555',
'(123) 456-7890',
'198 765 4321 ext. 888',
];
$scrubbed = $cleaner->scrubbers(['strip_phone_number'])->scrub($dirty);
/*
[
'5555555555',
'1234567890',
'1987654321x888',
];
*/
You can register custom scrubbers with Mr. Clean., (*23)
First, write your class. All you have to do is extend MrClean\Scrubber\BaseScrubber which adheres to MrClean\Scrubber\ScrubberInterface. There is a single property, value available to you. This is the string you will manipulate:, (*24)
namespace Your\Namespace;
use MrClean\Scrubber\BaseScrubber;
class YourCustomScrubber extends BaseScrubber {
public function scrub()
{
return str_replace('!', '.', $this->value);
}
}
And that's it. Now just register your scrubber with Mr. Clean., (*25)
The register method will take a string indicating the full path of the class, or an array of class paths., (*26)
$cleaner->register('Your\Namespace\YourCustomScrubber');
Now, go ahead and use it:, (*27)
$dirty = [
'I need to calm down!',
'Me too!',
];
$scrubbed = $cleaner->scrubbers(['your_custom_scrubber'])->scrub($dirty);
/*
[
'I need to calm down.',
'Me too.',
]
*/
MIT
MIT
MIT