Skip to content

Instantly share code, notes, and snippets.

@bencentra
Created August 17, 2013 01:47
Show Gist options
  • Save bencentra/6254856 to your computer and use it in GitHub Desktop.
Save bencentra/6254856 to your computer and use it in GitHub Desktop.
A basic whitelist to prevent XSS.
<?php
function xss_whitelist($input, $limit = null, $offset = 0)
{
// Force input to be a string0
$x = (string) $input;
// Allow alphanumeric characters, whitespace, and specific characters
$x = preg_replace("/[^a-zA-Z0-9 -:,.!?\/|]/", "",$x);
// Limit characters
if ($limit) {
$x = substr($x, $offset, $limit);
}
// Convert characters to HTML entities and return the sanitized string
return htmlentities($x, ENT_QUOTES, 'UTF-8');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment