Friday, May 29, 2009

PHP: htmlspecialchars on an array

Here is a helpful function to perform the htmlspecialchars function on all elements of an array. It will also perform it on arrays within the array.

Below it is the htmlspecialchars_decode version.


function array_htmlspecialchars(&$input)
{
if (is_array($input))
{
foreach ($input as $key => $value)
{
if (is_array($value)) $input[$key] = array_htmlspecialchars($value);
else $input[$key] = htmlspecialchars($value);
}
return $input;
}
return htmlspecialchars($input);
}

function array_htmlspecialchars_decode(&$input)
{
if (is_array($input))
{
foreach ($input as $key => $value)
{
if (is_array($value)) $input[$key] = array_htmlspecialchars_decode($value);
else $input[$key] = htmlspecialchars_decode($value);
}
return $input;
}
return htmlspecialchars_decode($input);
}

4 comments:

  1. use of array has enhanced my performance...thanks for posting such a useful information....
    PHP Programming

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete