note 102168 deleted from function.mysql-real-escape-string by philip
| From: | philip@php.net | Date: | Fri, 04 Sep 2015 21:42:47 +0000 |
| Subject: | note 102168 deleted from function.mysql-real-escape-string by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-203688@lists.php.net to get a copy of this message | ||
Note Submitter: stefan dot van dot beusekom at gmail dot com
----
Following is a recursive function that makes all the strings it finds safe for data entry. If the
underlying value is an array, it will loop through that array as well, to inifity. So any array
depth is allowed. This might come in handy when dealing with sessions, posts, gets or whatnot
arrays, where you immediately want to make use of the data without having to filter the data first.
<?php
/*
* Created by: Stefan van Beusekom
* Created on: 31-01-2011
* Description: A method that ensures safe data entry, and accepts either strings or arrays. If
the array is multidimensional,
* it will recursively loop through the array and make all points of data safe for entry.
* parameters: string or array;
* return: string or array;
*/
public function filterParameters($array) {
// Check if the parameter is an array
if(is_array($array)) {
// Loop through the initial dimension
foreach($array as $key => $value) {
// Check if any nodes are arrays themselves
if(is_array($array[$key]))
// If they are, let the function call itself over that particular node
$array[$key] = $this->filterParameters($array[$key]);
// Check if the nodes are strings
if(is_string($array[$key]))
// If they are, perform the real escape function over the selected node
$array[$key] = mysql_real_escape_string($array[$key]);
}
}
// Check if the parameter is a string
if(is_string($array))
// If it is, perform a mysql_real_escape_string on the parameter
$array = mysql_real_escape_string($array);
// Return the filtered result
return $array;
}
?>