note 110026 added to filter.filters.sanitize
| From: | googlybash24 at aol dot com | Date: | Thu, 13 Sep 2012 08:39:03 +0000 |
| Subject: | note 110026 added to filter.filters.sanitize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190678@lists.php.net to get a copy of this message | ||
This should help with most simple "textarea" fields in post forms.
Removing user html tags while maintaining text formatting such as newlines and carriage returns
involves using the FILTER_SANITIZE_STRING filter ID with the flag !FILTER_FLAG_STRIP_LOW. The
formatting text (the low ASCII values under decimal 32) are encoded because of the included
FILTER_FLAG_ENCODE_LOW flag, but you are now preventing these from being removed. When you want to
display the value on the page back in its intended format, use nl2br() so the encoded newlines are
formatted properly on the page.
This example cleans $_POST data from a textarea field with the name "user_tasks" on a
previous html form, stripping tags but maintaining formatting (at least for newlines):
<?php
$postfilter =
array(
'user_tasks' => array('filter' => FILTER_SANITIZE_STRING,
'flags' => !FILTER_FLAG_STRIP_LOW), // removes tags. formatting code is encoded -- add
nl2br() when displaying
);
$revised_post_array = filter_input_array(INPUT_POST, $postfilter); // must be referenced via a
variable which is now an array that takes the place of $_POST[]
echo (nl2br($revised_post_array['user_tasks'])); // here we use nl2br() for the displayed
value, for the ['user_tasks'] array value so that the newlines are formatted
?>
----
Server IP: 69.147.83.197
Probable Submitter: 71.172.140.197
----
Manual Page -- http://www.php.net/manual/en/filter.filters.sanitize.php
Edit -- https://master.php.net/note/edit/110026
Del: integrated -- https://master.php.net/note/delete/110026/integrated
Del: useless -- https://master.php.net/note/delete/110026/useless
Del: bad code -- https://master.php.net/note/delete/110026/bad+code
Del: spam -- https://master.php.net/note/delete/110026/spam
Del: non-english -- https://master.php.net/note/delete/110026/non-english
Del: in docs -- https://master.php.net/note/delete/110026/in+docs
Del: other reasons-- https://master.php.net/note/delete/110026
Reject -- https://master.php.net/note/reject/110026
Search -- https://master.php.net/manage/user-notes.php