htmlentities on an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

htmlentities on an array

Post by AGISB »

Whats the easiest way to use htmlentities to a complete array not knowing the elements of the array?

I am thinking foreach but it escapes me how to change the array value within the foreach.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: htmlentities on an array

Post by Chris Corbyn »

AGISB wrote:Whats the easiest way to use htmlentities to a complete array not knowing the elements of the array?

I am thinking foreach but it escapes me how to change the array value within the foreach.

Code: Select all

foreach ($array as $key => $value)
{
    $array[$key] = htmlentities($value);
}

print_r($array);
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I'd use..

Code: Select all

array_walk($array,create_function('&$s', '$s = htmlentities($s);'));
;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

http://us3.php.net/manual/en/function.array-map.php

Code: Select all

$array = array_map("htmlentities", $array);
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Jenk wins the contest =P :wink:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Sami wrote:Jenk wins the contest =P :wink:
Fully! What a great function :)
Post Reply