php - Deprecated each in code igniter xssclean helper


I have a codeigniter helper called xssclean for input validating form data

If i give array it show each deprecated error.

Here is my function in my xssclean_helper.php

function xssclean($str) {
if (is_array($str)) {
    while (list($key) = each($str)) {
        $str[$key] = $xssclean($str[$key]);
    return $str;
$str = _remove_invisible_characters($str);
$str = preg_replace('|\&([a-z\_0-9]+)\=([a-z\_0-9]+)|i', _xss_hash() . "\\1=\\2", $str);
$str = preg_replace('#(&\#?[0-9a-z]{2,})([\x00-\x20])*;?#i', "\\1;\\2", $str);
$str = preg_replace('#(&\#x?)([0-9A-F]+);?#i', "\\1\\2;", $str);
$str = str_replace(_xss_hash(), '&', $str);
$str = rawurldecode($str);
$str = preg_replace_callback("/[a-z]+=([\'\"]).*?\\1/si", '_convert_attribute', $str);
$str = preg_replace_callback("/<\w+.*?(?=>|<|$)/si", '_html_entity_decode_callback', $str);
$str = _remove_invisible_characters($str);
$str = _remove_tabs($str);
$str = _never_allowed_str($str);
$str = _never_allowed_regx($str);
$str = str_replace(array('<?', '?' . '>'), array('&lt;?', '?&gt;'), $str);
$str = _never_allowed_words($str);
do {
    $original = $str;
    if (preg_match("/<a/i", $str)) {
        $str = preg_replace_callback("#<a\s+([^>]*?)(>|$)#si", '_js_link_removal', $str);
    if (preg_match("/<img/i", $str)) {
        $str = preg_replace_callback("#<img\s+([^>]*?)(\s?/?>|$)#si", '_js_img_removal', $str);
    if (preg_match("/script/i", $str) OR preg_match("/xss/i", $str)) {
        $str = preg_replace("#<(/*)(script|xss)(.*?)\>#si", '', $str);
} while ($original != $str);
$event_handlers = array('[^a-z_\-]on\w*', 'xmlns');
$str            = preg_replace("#<([^><]+?)(" . implode('|', $event_handlers) . ")(\s*=\s*[^><]*)([><]*)#i", "<\\1\\4", $str);
$naughty        = 'alert|applet|audio|basefont|base|behavior|bgsound|blink|body|embed|expression|form|frameset|frame|head|html|ilayer|iframe|input|isindex|layer|link|meta|object|plaintext|style|script|textarea|title|video|xml|xss';
$str            = preg_replace_callback('#<(/*\s*)(' . $naughty . ')([^><]*)([><]*)#is', '_sanitize_naughty_html', $str);
$str            = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', "\\1\\2&#40;\\3&#41;", $str);
$str            = _never_allowed_str($str);
$str            = _never_allowed_regx($str);
return $str;


At line no 3 i get error




Theeach() function is deprecated with PHP 7.2. But you can replace your while-loop with a foreach-loop:

function xssclean($str) {
    if (is_array($str)) {
        foreach($str as &$value){
            $value = xssclean($value);

        return $str;
    // …

The$value variable is per default a copy of the array value. The& makes it a reference, this way you can update the value. Manipulating the array while iterating over it, is not a good idea and can lead to errors.

People are also looking for solutions to the problem: php - Codeigniter : Check if file_exist in Cpanel


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.