PHP code example of joetannenbaum / mr-clean

1. Go to this page and download the library: Download joetannenbaum/mr-clean library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

joetannenbaum / mr-clean example snippets




$cleaner = new MrClean\MrClean();

$scrubbers = [
    'trim',
    'stripslashes',
    'strip_tags',
    'remove_weird_characters',
];

$scrubbed = $cleaner->scrubbers($scrubbers)->scrub('I\'m not that dirty.');

$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.')

$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'],
        ],
    ]);

$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 &amp; Beans',
    ],
]
*/

$scrubbers = [
    'strip_tags',
    'first_name' => ['trim'],
    'last_name'  => ['stripslashes', 'htmlentities'],
    'htmlspecialchars',
];

$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,
];
*/

$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>

$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>

$dirty = [
    'cool',
    'also cool',
    ' ',
    '    ',
];

$scrubbed = $cleaner->scrubbers(['nullify'])->scrub($dirty);

/*
[
    'cool',
    'also cool',
    null,
    null,
];
*/

$dirty = [
    '11111111',
    '22',
    'bbbbbbbb',
    '333334',
];

$scrubbed = $cleaner->scrubbers(['null_if_repeated'])->scrub($dirty);

/*
[
    null,
    '22',
    null,
    '333334',
];
*/

$dirty = [
    '555-555-5555',
    '(123) 456-7890',
    '198 765 4321 ext. 888',
];

$scrubbed = $cleaner->scrubbers(['strip_phone_number'])->scrub($dirty);

/*
[
    '5555555555',
    '1234567890',
    '1987654321x888',
];
*/

namespace Your\Namespace;

use MrClean\Scrubber\BaseScrubber;

class YourCustomScrubber extends BaseScrubber {

    public function scrub()
    {
        return str_replace('!', '.', $this->value);
    }

}

$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.',
]
*/