How to loop htmlspecialchars on array in array in array in - php

I'm creating a function that "cleans" data (escapes html to avoid xss) from the database before sending it to the views. All the data is passed on in 1 array. This arrays contains variables and arrays that contain other variables and arrays, and so on.
This is what I have now, it works, but it just doesn't look right. Is there any way to avoid going through a new foreach for every array inside an array?
public function clean_output(&$data)
{
if(!is_array($data))
{
$data = htmlspecialchars($data);
}
else
{
foreach($data as &$data_1)
{
if(!is_array($data_1))
{
$data_1 = htmlspecialchars($data_1);
}
else
{
foreach($data_1 as &$data_2)
{
if(!is_array($data_2))
{
$data_2 = htmlspecialchars($data_2);
}
else
{
foreach($data_2 as &$data_3)
{
$data_3 = htmlspecialchars($data_3);
}
}
}
}
}
}
}
Thanks to Antoine, I got a new function. Suggestions still welcome offcourse!
public function clean_output(&$data)
{
if(!is_array($data))
{
$data = htmlspecialchars($data);
}
else
{
foreach($data as &$data_1)
{
$this->clean_output($data_1);
}
}
}

You must use recursive function !
http://www.elated.com/articles/php-recursive-functions/

Related

How to pass the variable names as they are in array

<?php
function empty_values()
{
$values = func_get_args();
while (list(, $value) = each($values))
{
if (empty($value))
{
return false;
}
}
return true;
}
?>
I want to pass the values from $_POST like this
if (empty_values(implode("," , $_POST)
{
//some code
}
Use array_filter() instead and compare the length to the length of the $_POST array.
if (count($_POST) > count(array_filter($_POST))) {
// At least one value was empty
}

My sanitizing $_POST[] function also sanitizes Turkish characters

I've found a function on web to sanitize user input data and used it for creating an alternative to $_POST[] method as post() . However, it seems that this function also sanitizes UTF-8 characters such as ç,ş,ö,ı,İ,Ö,ğ, converting them into strings like öl. I don't know which part of the code does that.
Thanks in advance.
Sanitizing function
function post($key) {
if (isset($_POST[$key])) {
$data = $_POST[$key];
if (is_array($data)) {
foreach ($data as $key => $element) {
$data[$key] = filter($element);
}
} else {
$data = trim(htmlentities(strip_tags($data)));
if(get_magic_quotes_gpc())
$data = stripslashes($data);
$data = pg_escape_string($data);
}
return $data;
} else {
return false;
}
}
Looking at the manual I think you would need to add some additional params to your htmlentities call to let it know you are using UTF-8 encoded strings.
Here is a possible solution, I factored the relevant portion out into a separate function for clarity.
function post($key){
if (isset($_POST[$key])){
$post = $_POST[$key];
if (is_array($post)) {
$data = array();
foreach ($post as $key => $element) {
$data[$key] = filter($element);
}
} else {
$data = formatHtmlEntities($post);
}
return $data;
}
return false;
}
function formatHtmlEntities($data)
{
$stripTags = strip_tags($data);
$entityEncodedData = trim(htmlentities($stripTags, ENT_QUOTES, "UTF-8"));
if (get_magic_quotes_gpc()) {
$entityEncodedData = stripslashes($entityEncodedData);
}
return pg_escape_string($entityEncodedData);
}
I've found an alternative solution which is to use htmlspecialchars() vs htmlentities() for my case. Reference question: htmlentities() vs. htmlspecialchars()

Finding for string in an Array

My problem is basically, I will be trying to get data from a table called, 'rank' and it will have data formatted like, "1,2,3,4,5" etc to grant permissions. So Basically I am trying to make it an array and find if one number is there in the array. Basically making it an array is not working. How would I get this done? Here is my code below:
<?php
function rankCheck($rank) {
$ranks = "1,2,3,4,5";
print_r($uRanks = array($ranks));
if(in_array($rank, $uRanks)) {
return true;
} else {
return false;
}
}
if(rankCheck(5) == true) { echo "Hello"; } else { echo "What?"; }
?>
This code returns false, while it should return true. This is just a basic algorithm.
The print_r Display:
Array ( [0] => 1,2,3,4,5 )
If you know for sure your delimiter is a comma, try this:
$ranks = explode(',',$rank);
where $rank is your string.
That's simple, you explode the $ranks variable by comma ,:
$ranks = "1,2,3,4,5";
$uRanks = explode(',',$ranks);
//$uRanks would now be array(1,2,3,4,5);
if(in_array($rank, $uRanks)) {
//..rest of your code
You should:
$uRanks = explode(',', $ranks);
instead of:
$uRanks = array($ranks);
to make this as array.
Problem solved. I used the explode function instead like this:
<?php
function rankCheck($rank) {
$ranks = "1,2,3,4,5";
print_r($uRanks = explode(',', $ranks));
if(in_array($rank, $uRanks)) {
return true;
} else {
return false;
}
}
if(rankCheck(5) == true) { echo "Hello"; } else { echo "What?"; }
?>

How to check if all values in multidimensional array are empty

I have a form posting a multidimensional array to my PHP script, I need to know if all the values in the array are empty or not.
Here is my array:
$array[] = array('a'=>'',
'b'=>array('x'=>''),
'c'=>array('y'=>array('1'=>'')),
'd'=>'');
I tried using array_reduce(), but it's just returning an array:
echo array_reduce($array, "em");
function em($a,$b){
return $a.$b;
}
Any ideas?
I noticed this has been hanging around for a while, this is a custom function that works quite well.
function emptyArray($array) {
$empty = TRUE;
if (is_array($array)) {
foreach ($array as $value) {
if (!emptyArray($value)) {
$empty = FALSE;
}
}
}
elseif (!empty($array)) {
$empty = FALSE;
}
return $empty;
}
if all items in the array is empty then the function will return true, but if one item in the array is not empty then the function will return false.
Usage:
if (emptyArray($ARRAYNAME)) {
echo 'This array is empty';
}
else {
echo 'This array is not empty';
}

PHP looping multidimensional array

Not entirely sure how to adequately title this problem, but it entails a need to loop through any array nested within array, of which may also be an element of any other array - and so forth. Initially, I thought it was required for one to tag which arrays haven't yet been looped and which have, to loop through the "base" array completely (albeit it was learned this isn't needed and that PHP somehow does this arbitrarily). The problem seems a little peculiar - the function will find the value nested in the array anywhere if the conditional claus for testing if the value isn't found is omitted, and vice versa. Anyway, the function is as followed:
function loop($arr, $find) {
for($i=0;$i<count($arr);$i++) {
if($arr[$i] == $find) {
print "Found $find";
return true;
} else {
if(is_array($arr[$i])) {
$this->loop($arr[$i], $find);
} else {
print "Couldn't find $find";
return false;
}
}
}
}
Perhaps you should change your code to something like:
var $found = false;
function loop($arr, $find) {
foreach($arr as $k=>$v){
if($find==$v){
$this->found = true;
}elseif(is_array($v)){
$this->loop($v, $find);
}
}
return $this->found;
}
This has been working for me for a while.
function array_search_key( $needle_key, $array ) {
foreach($array AS $key=>$value){
if($key == $needle_key) return $value;
if(is_array($value)){
if( ($result = array_search_key($needle_key,$value)) !== false)
return $result;
}
}
return false;
}
OK, what about a slight modification:
function loop($arr, $find) {
for($i=0;$i<count($arr);$i++) {
if(is_array($arr[$i])) {
$this->loop($arr[$i], $find);
} else {
if($arr[$i] == $find) {
print "Found $find";
return true;
}
}
}
return false;
}
Hmm?
Try this: PHP foreach loop through multidimensional array

Categories