All the HEX code must be replaced or else some XSS attacks can be successful
diff --git a/system/core/Security.php b/system/core/Security.php
index 4f2185d..220188e 100644
--- a/system/core/Security.php
+++ b/system/core/Security.php
@@ -526,9 +526,17 @@
 			$charset = config_item('charset');
 		}
 
-		$str = html_entity_decode($str, ENT_COMPAT, $charset);
-		$str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str);
-		return preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str);
+                do
+                {
+                    $matches = $matches1 = 0;
+                    
+                    $str = html_entity_decode($str, ENT_COMPAT, $charset);
+                    $str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str, -1, $matches);
+                    $str = preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str, -1, $matches1);
+                }
+                while($matches || $matches1);
+
+                return $str;
 	}
 
 	// --------------------------------------------------------------------