I am using this php code to remove backslashes in my array:
$data[] = $_POST;
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
$data = stripslashes_deep($data);
but I still have a backslash before the slashes like this:
"2''1\/2"
Can you please help remove this backslash in php.
Thanks
This will work, to remove \
$value= preg_replace('#\\\\#','',$value);
function stripslashes_deep($item){
return stripslashes($item);
}
if(is_array($data)){
$value=array_map('stripslashes_deep', $data);
}
else{
$value=stripslashes($data);
}
I found it's much simpler using a foreach
foreach ($data as $key=>$value) {
$data[$key] = stripslashes($value);
}
instead of the conditional operator and the recursive call..
Related
I need to stripslashes all items of an array at once.
Any idea how can I do this?
foreach ($your_array as $key=>$value) {
$your_array[$key] = stripslashes($value);
}
or for many levels array use this :
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);
print_r($array);
For uni-dimensional arrays, array_map will do:
$a = array_map('stripslashes', $a);
For multi-dimensional arrays you can do something like:
$a = json_decode(stripslashes(json_encode($a)), true);
This last one can be used to fix magic_quotes, see this comment.
You can use array_map:
$output = array_map('stripslashes', $array);
I found this class / function
<?php
/**
* Remove slashes from strings, arrays and objects
*
* #param mixed input data
* #return mixed cleaned input data
*/
function stripslashesFull($input)
{
if (is_array($input)) {
$input = array_map('stripslashesFull', $input);
} elseif (is_object($input)) {
$vars = get_object_vars($input);
foreach ($vars as $k=>$v) {
$input->{$k} = stripslashesFull($v);
}
} else {
$input = stripslashes($input);
}
return $input;
}
?>
on this blog and it really helped me, and now i could pass variables, arrays and objects all through the same function...
Parse array recursevely, with this solution you don't have to dublicate your array
function addslashes_extended(&$arr_r){
if(is_array($arr_r))
{
foreach ($arr_r as &$val){
is_array($val) ? addslashes_extended($val):$val=addslashes($val);
}
unset($val);
}
else
$arr_r=addslashes($arr_r);
return $arr_r;
}
Any recursive function for array :
$result= Recursiver_of_Array($array, 'stripslashes');
code:
function Recursiver_of_Array($array,$function_name=false){
//on first run, we define the desired function name to be executed on values
if ($function_name) { $GLOBALS['current_func_name']= $function_name; } else {$function_name=$GLOBALS['current_func_name'];}
//now, if it's array, then recurse, otherwise execute function
return is_array($array) ? array_map('Recursiver_of_Array', $array) : $function_name($array);
}
I need to sanitize the values in a JSON file (e.g., a composer.json file from github). I json_decode($file) converting it to a stdClass object. (I need it as an object, not as an array - I am aware of that option).
I need to recursively sanitize all the values which might be objects as well (and maybe the keys too?).
I need to remove any and all "dangerous" characters, etc from the file but would like it to remain multilingual, so was planning to use filter_var($value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW).
Advice and suggestions please. maybe I'm missing the obvious, but this seems harder than it should.
Object can be iterated by foreach:
function sanitize($data) {
foreach ($data as &$value) {
if (is_scalar($value)) {
$value = sanitizeValue($value);
continue;
}
sanitize($value);
}
return $data;
}
Answer by Михаил-М was close. I needed to adjust it slightly to be:
function sanitize($data) {
foreach ($data as &$value) {
if (is_scalar($value)) {
$value = sanitizeValue($value);
continue;
}
$value = sanitize($value);
}
return $data;
}
of course, this doesn't address the issue of actually sanitizing the data which I did with the filter_var method I mentioned above. so I finally solved it with this:
function sanitize($data) {
foreach ($data as &$value) {
if (is_scalar($value)) {
$value = filter_var($value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
continue;
}
$value = sanitize($value);
}
return $data;
}
Is there a function that works like $_GET ?
I mean a function that transforms
"?var1=5&var2=true"
to
$var1=5;
$var2="true";
So that I can use one variable (string) in a function and fetch many variables from it?
Like:
function manual_GET($args){ /* ? */}
function myFunction($args)
{
manual_GET($args);
if(isset($var1))/* doesn't have to be this way, btw */
{
do_something($var1);
}
//etc
}
p.s. : I don't want to use $_GET with URL because this file is a class file (namely database_library.php) so I don't execute it directly, or make an AJAX call. I just require_once(); it.
Yes, there is. It is called parse_str: http://php.net/manual/en/function.parse-str.php
One way to fix it.
function myFunction($args){
return parse_str($args,$values);
}
function parseQueryString($str) {
$op = array();
$pairs = explode("&", $str);
foreach ($pairs as $pair) {
list($k, $v) = array_map("urldecode", explode("=", $pair));
$op[$k] = $v;
}
return $op;
}
it works like parse_str but doesn't convert spaces and dots to underscores
"?var1=5&var2=true"
foreach ($_GET AS $k=>$v){
$$k=$v;
}
echo $var1; // print 5
echo $var2; // print true
Need you help in an unusal situation. I need to trim all the $_POST variables.
Is there any way I can do it at a single shot i.e., using a single function?
I know trim($_POST) won't do, I have to make some function like
function sanatize_post(){
foreach ($_POST as $key => $val)
$_POST[$key] = trim($val);
}
But, if you have any other suggestion or comment, please help me.
Thanks
Simply
$_POST = array_map("trim", $_POST);
But if $_POST members (even if 1 of them) is again an array itself, then use recursive version:
function array_map_deep( $value, $callback )
{
if ( is_array( $value ) ) {
foreach ( $value as $index => $item ) {
$value[ $index ] = array_map_deep( $item, $callback );
}
} elseif ( is_object( $value ) ) {
$object_vars = get_object_vars( $value );
foreach ( $object_vars as $property_name => $property_value ) {
$value->$property_name = array_map_deep( $property_value, $callback );
}
} else {
$value = call_user_func( $callback, $value );
}
return $value;
}
Here's a one-liner that will also work either on single values or recursively on arrays:
$_POST = filter_var($_POST, \FILTER_CALLBACK, ['options' => 'trim']);
use array_walk with a custom function
$clean_values = array();
array_walk($_POST, 'sanitize_post');
function sanitize_post($item, $key)
{
$clean_values[$key] = trim($item);
//optional further cleaning ex) htmlentities
}
array_walk($_POST, 'trim') (note that this and the idea might be broken as input name=foo[bar] is translated into an array)
Edit: the above is not correct. Try $_POST = array_map('trim', $_POST);.
You can also use this code I wrote in case you want to sanitize a string OR an array with one function:
function sanitize ($value) {
// sanitize array or string values
if (is_array($value)) {
array_walk_recursive($value, 'sanitize_value');
}
else {
sanitize_value($value);
}
return $value;
}
function sanitize_value (&$value) {
$value = trim(htmlspecialchars($value));
}
Simply use it like this:
$post_sanitized = sanitize($_POST);
$apple_sanitized = sanitize('apple');
Simply use this:
array_walk($_POST, create_function('&$val', '$val = trim($val);'));
and your $_POST is now trimmed.
The other answers did not work well with associative arrays. This recursive functions will trim all values inside a $_POST array all levels down.
// Trim all values of associative array
function trim_associative_array(&$input_array) {
if (is_array($input_array)) {
foreach ($input_array as $key => &$val) {
if (is_array($val)) {
trim_associative_array($val);
} else {
$input_array[$key] = trim($val);
}
}
}
}
I think it's better to use anonymous functions :
array_walk($_POST, function(& $value){
$value = trim($value);
});
I am using an eCard program here to send invitations for an event and get the following notice:
Notice: Array to string conversion in
/nfs/c07/h01/mnt/108712/domains/christmasnativity.org/html/ecard/include/common.inc.php
on line 32
Here is the code from lines 29 to 33:
/* Clean up request: Remove magic quotes, if the setting is enabled. */
if (get_magic_quotes_gpc()) {
foreach($_REQUEST as $name => $value)
$_REQUEST[$name] = stripslashes($value);
}
Any clues what may be causing this error notice?
Thanks.
One of the values in $_REQUEST is an array. This can happen if a variable uses a name such as foo[].
You can avoid running stripslashes on arrays like this
if (get_magic_quotes_gpc()) {
foreach($_REQUEST as $name => $value)
if(!is_array($value)){
$_REQUEST[$name] = stripslashes($value);
}
}
but then the values inside an array $value won't get stripped.
A more complete solution would be something like this:
if (get_magic_quotes_gpc())
{
strip_slashes_recursive($_REQUEST);
}
function strip_slashes_recursive(&$array)
{
foreach ($array as $key => $value)
{
if (is_array ($value))
{
strip_slashes_recursive ($array[$key]);
}
else
{
$array[$key] = stripslashes($value);
}
}
}
Like Ignacio Vazquez-Abrams says, one of the $value's is an array. You can use the following to see what is an array (assuming you are/can output the results to somewhere you can see them):
$_REQUEST[$name] = stripslashes($value);
var_dump($value);