Download the PHP package joetannenbaum/mr-clean without Composer
On this page you can find all versions of the php package joetannenbaum/mr-clean. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mr-clean
Mr. Clean
Mr. Clean is an extendible PHP cleaner that allows you to easily clean up strings, arrays, objects, and anything in between.
Table of Contents
- Installation
- Basic Usage
- Scrubbers
- Pre/Post
- What Can Be Cleaned
- Cleaning Specific Keys
- Available Scrubbers
- Boolean
- HTML
- Strip CSS Attributes
- Nullify
- Null If Repeated
- Strip Phone Number
- Extending
- Writing a Scrubber
- Registering a Scrubber
Installation
Using composer:
Basic Usage
Fire it up like so:
Scrubbers
Scrubbers are the classes and functions that actually do the work, and you can assign as many as you want to clean your object.
Scrubbers should always be passed as an array, and will be run in the order that you specify.
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
.
Pre/Post
To save some typing, you can set scrubbers to run every time before and after each cleaning:
What Can Be Cleaned
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:
Cleaning Specific Keys
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:
You can also still specify scrubbers that should run for everything:
Available Scrubbers
Mr. Clean comes with a bevy of pre-built scrubbers you can use:
Boolean
Converts falsey text and anything considered empty
to false
, otherwise returns true
. Falsey text includes (not case sensitive):
- no
- n
- false
HTML
Strips tags not on the whitelist, removes empty content tags, and repeated opening or closing tags. The whitelist includes:
- a
- p
- div
- strong
- em
- b
- i
- br
- ul
- ol
- li
- h1
- h2
- h3
- h4
- h5
- h6
Strip CSS Attributes
Strips the style
, class
, and id
attributes off of all HTML elements.
Nullify
If a trimmed string doesn't have any length, null it out:
Null If Repeated
If a string is just a repeated character ('1111111' or 'aaaaaaaaa') and has a length greater than two, null it out:
Strip Phone Number
Strip a phone number down to just the good bits, numbers and the letter 'x' (for extensions):
Extending
You can register custom scrubbers with Mr. Clean.
Writing a Scrubber
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:
And that's it. Now just register your scrubber with Mr. Clean.
Registering a Scrubber
The register
method will take a string indicating the full path of the class, or an array of class paths.
Now, go ahead and use it: