Skip to content

Instantly share code, notes, and snippets.

@racibaz
Last active June 24, 2019 06:54
Show Gist options
  • Save racibaz/10863c9f51064d1d9390016b28143f85 to your computer and use it in GitHub Desktop.
Save racibaz/10863c9f51064d1d9390016b28143f85 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: oLDu67
* Date: 19.09.2017
* Time: 10:44
*
* @param array $inputs
* @param array|null $excepts
*
* @return array
*/
function removeHtmlTagsOfFields(array $inputs, array $excepts = null)
{
$inputOriginal = $inputs;
$inputs = array_except($inputs, $excepts);
foreach ($inputs as $index => $in){
$inputs[$index] = strip_tags($in);
}
if(!empty($excepts)){
foreach ($excepts as $except){
$inputs[$except] = $inputOriginal[$except];
}
}
return $inputs;
}
/**
* @param string $field
*
* @return string
*/
function removeHtmlTagsOfField(string $field){
return htmlentities(strip_tags($field), ENT_QUOTES, 'UTF-8');
}
@racibaz
Copy link
Author

racibaz commented Sep 19, 2017

    /*
     * Usage :
     *
     *
        Remove tags of array fields in controller with excepts fields


        $inputs = removeHtmlTagsOfFields(Input::all(),[
                                '_method',
                                '_token',
                                'password'
                            ]);


        $inputs = removeHtmlTagsOfFields($request->all(),[
                                        '_method',
                                        '_token',
                                        'password'
                                   ]);

        Instead of

        $input['facebook'] = strip_tags($input['facebook']);
        $input['twitter'] = strip_tags($input['twitter']);
        $input['pinterest'] = strip_tags($input['pinterest']);
        $input['linkedin'] = strip_tags($input['linkedin']);
        $input['youtube'] = strip_tags($input['youtube']);
        $input['web_site'] = strip_tags($input['web_site']);
        $input['bio_note'] = strip_tags($input['bio_note']);
        ....

        Optional excepts

        $inputs = removeHtmlTagsOfFields($request->all());


        https://gist.github.com/oLDu67/10863c9f51064d1d9390016b28143f85
     *
     *
     * */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment