array walk?

array walk?

am 28.06.2007 16:12:36 von Juan Ignacio Borda

this code doesn't work on my PHP Version 5.2.1 (i'm trying to protect a
stie from XSS)

$a=Array();
$a[]='como';
$a[]=Array('hola','hello');
array_walk_recursive($a,'strip_tags');
echo "

";
var_dump($a);
echo "
";
?>
it outputs:

array(2) {
[0]=>
string(11) "como"
[1]=>
&array(2) {
[0]=>
string(11) "hola"
[1]=>
string(5) "hello"
}
}


any clues?

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: array walk?

am 28.06.2007 18:44:01 von Michael Southworth

------=_Part_15514_24677894.1183049041161
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Looks like the answer is in the manual entry for array_walk_recursive

http://php.net/array_walk_recursive

*Note: * If *funcname* needs to be working with the actual values of the
array, specify the first parameter of *funcname* as a
reference.
Then, any changes made to those elements will be made in the original array
itself.

Try using a wrapper around strip_tags which tags a reference as its first
argument
function array_walk_strip_tags(&$string){
$string = strip_tags($string);
}

PHP is essentially only passing a copy of $a[i] and stripping the tags from
the copy without changing the actual values of $a. Passing by reference
ensures that you modify the entries in $a itself. How this is handled below
the hood is slightly more complex and I don't have a ton of experience with
how PHP specifically handles that sort of thing, but it looks like that will
at least point you in the right direction.

-Michael



On 6/28/07, Juan Ignacio Borda wrote:
>
> this code doesn't work on my PHP Version 5.2.1 (i'm trying to protect a
> stie from XSS)
>
> > $a=Array();
> $a[]='como';
> $a[]=Array('hola','hello');
> array_walk_recursive($a,'strip_tags');
> echo "

";
> var_dump($a);
> echo "
";
> ?>
> it outputs:
>
>
array(2) {
> [0]=>
> string(11) "como"
> [1]=>
> &array(2) {
> [0]=>
> string(11) "hola"
> [1]=>
> string(5) "hello"
> }
> }
>

>
> any clues?
>
> --
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

------=_Part_15514_24677894.1183049041161--

Re: array walk?

am 07.07.2007 13:10:46 von El Bekko

Take a look at array_walk()
http://be2.php.net/manual/en/function.array-map.php

It might be better for what you're doing.

Juan Ignacio Borda wrote:
> this code doesn't work on my PHP Version 5.2.1 (i'm trying to protect a
> stie from XSS)
>
> > $a=Array();
> $a[]='como';
> $a[]=Array('hola','hello');
> array_walk_recursive($a,'strip_tags');
> echo "

";
> var_dump($a);
> echo "
";
> ?>
> it outputs:
>
>
array(2) {
> [0]=>
> string(11) "como"
> [1]=>
> &array(2) {
> [0]=>
> string(11) "hola"
> [1]=>
> string(5) "hello"
> }
> }
>

>
> any clues?

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php