What does this function do?

Discussion in 'PHP' started by BANAGO, May 24, 2011.

  1. #1
    Hi Guys,

    What does this function do?

    function filterData($data){
    	if(is_array($data))
    		$data = array_map("filterData", $data);
    	else		
    		$data = htmlentities($data, ENT_QUOTES, 'UTF-8');
    	
    	return $data;
    }
    PHP:
     
    BANAGO, May 24, 2011 IP
  2. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #2
    It's a simple recursive function to go through an array and convert applicable characters to html entities.

    If you run the code below it'll give you a better understanding of what's going on, I changed htmlentities() to strtoupper() to give you a better idea.

    <?php
    function filterData($data){
        if(is_array($data))
            $data = array_map("filterData", $data);
        else        
            $data = strtoupper($data);
        
        return $data;
    }
    
    
    $array = array(
    	'foo' => array(
    		'baz' => 'word',
    		'boo' => array(
    			'bar' => 'hmm, hey!',
    			'boo' => 'whats up?',
    		),
    	),
    	'bing' => 'this is lowercase.. but will appear uppercase',
    );
    
    
    echo '<pre>';
    print_r(filterData($array));
    ?>
    PHP:
     
    crazyryan, May 24, 2011 IP
  3. BANAGO

    BANAGO Active Member

    Messages:
    456
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Thanks Ryan! I assumed the same thing, but was not sure about the recursion.
     
    BANAGO, May 24, 2011 IP