Skip to content

Instantly share code, notes, and snippets.

@sycobuny
Created September 13, 2013 17:12
Show Gist options
  • Save sycobuny/6553392 to your computer and use it in GitHub Desktop.
Save sycobuny/6553392 to your computer and use it in GitHub Desktop.
A quick hack to make it easier to sanitize outputs en masse before displaying them to the user.
<?php
function sanitize_for_html(&$entity, $flags = ENT_QUOTES) {
if (is_array($entity)) {
$iterator = $entity;
}
elseif (is_object($entity)) {
$iterator = get_object_vars($entity);
}
else {
$entity = htmlentities($entity, $flags);
return $entity;
}
foreach ($iterator as $field => $value) {
$clean = sanitize_for_html($value);
if (is_array($entity)) {
$entity[$field] = $value;
}
else {
$entity->$field = $value;
}
}
return $entity;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment