3

Can I run the $_POST array through a loop to get the same result as the following code?

$_POST['manufacturer'] = strip_tags($_POST['manufacturer']);<br>
$_POST['part_no']      = strip_tags($_POST['part_no']);<br>
$_POST['product_name'] = strip_tags($_POST['product_name']);<br>
$_POST['link']         = strip_tags($_POST['link']);<br>

Or is there a way to apply strip_tags() to every field in the $_POST array?

2
  • I have made these changes $form = array_map("strip_tags", $_POST); $form = array_map("trim", $form); This may be a new question, can I use array_map to remove all white spaces in an array too and if so how would I write that. Commented Mar 21, 2016 at 23:06
  • Possible duplicate of Easy way to apply a function to an array Commented Jul 18, 2018 at 2:18

4 Answers 4

6

Yes, use array_map()

$_POST = array_map("strip_tags", $_POST);
Sign up to request clarification or add additional context in comments.

5 Comments

I'd prefer to output it to a new variable name. It's better if you leave $_POST itself unaltered, because it avoids any confusion later.
This is what OP wants actually, but, yes I'd recommend also to put the result in a new variable.
I have made these changes <p>$form = array_map("strip_tags", $_POST);<br>$form = array_map("trim", $form);</p><bk> This may be a new question, can I use array_map to remove all white spaces in an array too and if so how would I write that.
Yes you can write it like that ( make sure to put the php code between php tags <?php ?> ). You can also nest them like this $form = array_map("trim", array_map("strip_tags", $_POST));
be mindful, if your array contains an element who's value is an array, this will keep the element's key in the array but make its value null
1

As previously stated, array_map() is the function you're looking for

However, be aware that if one of your array values is itself an array, strip_tags will nullify that value. The key will remain in the parent array, but the value of that key will be null. Here's a function I use in place of array_map to avoid this. A possible use is if you have html input field arrays in your $_POST. For example, several inputs with a name like name="hello[]"

function strip_tags_array($array){
    $result=array();
    if(is_array($array)){
        foreach($array as $key=>$value){
            if(is_array($value)){
                $result[$key]=strip_tags_array($value);
            }
            else if(is_string($value)){
                $result[$key]=strip_tags($value);
            }
            else{
                $result[$key]=$value;
            }
        }
    }
    return $result;
}

$_POST = strip_tags_array($_POST);

1 Comment

Good start, but had to change it a bit as this made arrays out of each value even if string as per the first line in the function.
0

Could not get any of the examples to work on my server. Are you folks testing solutions? This works for me:

// no code $allowcode=""; // put allowed code tags here foreach ($_POST as $key => $value) { $$key=strip_tags($value, $allowcode); } foreach ($_GET as $key => $value) { $$key=strip_tags($value, $allowcode); }

Comments

-1

If you do not want to use any PHP inbuilt function, you can do like below:

    foreach($_POST AS $key=>$val)
    {
      $_POST[$key] = strip_tags($val);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.