6

In drupal we have check_plain function to filter the unwanted codes from a text, I have an array which I am storing directly, in that case, I need to check each value before storing, is there a function to which we can pass the array and it should filter and reset the array.

Ex - I have a form where users can add as many names as they want using (multiple textfield) the name of the textfield is - guarantors[]

So I have an array where all the userinput is there, inorder to filter, I need to filter each value of array using foreach.

is there a function or way, so that I can directly pass the array to a function and it should return me an array with safe values, i.e each values should be parsed using check_plain function.

I think an expample may help you answer better, here is my example array.

Array
(
[0] => Array
    (
        [keyword] => lucidus
        [color] => #00ff00
    )

[1] => Array
    (
        [keyword] => commoveo
        [color] => #998899
    )

[2] => Array
    (
        [keyword] => dolus
        [color] => #ff0099
    )

)

NOTE :: keyword and color are user input values. And those needs to be filtered, it is and can be a multidimensional array.

Many Thanks

1

This is an old question, but there didn't seem to be a decent answer yet...

There is no out-of-the-box solution but it's easy to write a function that recursively walks the array and applies a filter function to the keys and values.

<?php
function clean_array($array, $filter = 'check_plain') {
  $clean = array();
  foreach ($array as $key => $val) {
    if (is_array($val)) {
      $clean[$filter($key)] = clean_array($val, $filter);
    }
    else {
      $clean[$filter($key)] = $filter($val);
    }
  }
  return $clean;
}
?>

This also gives you the flexibility to use filter_xss or check_markup instead.

Hope that helps.

0

I think array map is what you are looking for. This function takes a callback and an array as arguments, returns an array of all the processed values.

Code example, ripped out of php.net man page:

<?php
function cube($n)
{
    return($n * $n * $n);
}

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?>

Content of $b will be : Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 )

Hope this helps.

  • I think it is only working for single dimentional array, in my case, you can see the above example array which is a multidimensional array needs to be filtered. Some function which can traverse through all array values and reset them the way array_map does will be great. – Patri Dharmendra Oct 4 '12 at 11:59
0

You can do it using PHP function array_filter .

Find the example below

<?php
  function myfunction($v)  {
    $os = array("Dog", "Cat");
    if (in_array($v, $os)) {
      return true;
    }        
    return false;
  }
  $a=array(0=>"Dog",1=>"Cat",2=>"Horse");
  print_r(array_filter($a,"myfunction"));
?>

// Above example will return an array with Dog & Cat as values !

  • The question is about check_plain which is a small function that htmlspecialchars() with some arguments. Plus, array_filter takes boolean values. This will for validate stuff but not for convert-to-plain-text functions like check_plain. – AyeshK Oct 4 '12 at 11:21
  • so no default drupal function for that and we need to write our own ? – Patri Dharmendra Oct 4 '12 at 11:41
  • Drupal uses php language ! So, no need to hesitate using php functions :-) array_filter and in_array will solve your issue ! – Anil Sagar Oct 4 '12 at 11:46
  • I got it Anil Sagar, but my problem is multidimensional array, and I dont want to return true/false, I am in need of something like array_map but it should traverse through all values, I can write custom foreach to iterate through all arrays in PHP, but wondering if anything inbuilt is there. – Patri Dharmendra Oct 4 '12 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.