1

我在一个旧的 HTML 网站上有一个查询表,它在旧版本的 PHP 上运行良好。现在的问题是构成代码一部分的函数 eregi() 在所有新版本的 PHP 中都已弃用。

我不会假装我明白这是怎么回事!:)

这是下面的现有代码 - 它包含 eregi() 位:

// check for any human hacking attempts
class clean {
    function comments($message) {
        $this->naughty = false;
        $this->message = $message;
        $bad = array("content-type","bcc:","to:","cc:","href");
        $for = array( "\r", "\n", "%0a", "%0d");
        foreach($bad as $b) {
            if(eregi($b, $this->message)) {
                $this->naughty = true;
            }   
        }   
        $this->message = str_replace($bad,"#removed#", $this->message);
        $this->message = stripslashes(str_replace($for, ' ', $this->message));
        
        // check for HTML/Scripts
        $length_was = strlen($this->message);
        $this->message = strip_tags($this->message);
        if(strlen($this->message) < $length_was) {
            $this->naughty = true;
        }
   }
} // class

谷歌搜索后,我猜我需要用 preg_match 替换 eregi() 位?

我不知道在上面的代码中把它放在哪里才能工作?

有人有什么想法吗?

在此先感谢,亲切的问候

布赖恩

4

3 回答 3

1

您示例中的 eregi 函数仅用于简单的字符串比较。您可以简单地将其替换为条带:

if (stripos($this->message, $b) !== false) {
    $this->naughty = true;
}
于 2021-09-28T07:56:10.940 回答
0

像这样使用它

class clean {
    function comments($message) {
        $this->naughty = false;
        $this->message = $message;
        $bad = array("content-type","bcc:","to:","cc:","href");
        $for = array( "\r", "\n", "%0a", "%0d");
        foreach($bad as $b) {
            if (preg_match("/$b/i", $this->message)) {
                $this->naughty = true;
            } else {
                //comment does not contain that string.
            }
            //if(eregi($b, $this->message)) {
                //$this->naughty = true;
            //}
        }   
        $this->message = str_replace($bad,"#removed#", $this->message);
        $this->message = stripslashes(str_replace($for, ' ', $this->message));
        
        // check for HTML/Scripts
        $length_was = strlen($this->message);
        $this->message = strip_tags($this->message);
        if(strlen($this->message) < $length_was) {
            $this->naughty = true;
        }
   }
}
于 2021-09-28T07:57:01.147 回答
0

我只找到了文档的罗马尼亚语页面eregi,这似乎表明它自 PHP 5.3 以来已被弃用并在 7.0 中删除。

由于其目的是执行不区分大小写的正则表达式检查,您可以将其替换preg_match()i标志(代表“不区分大小写”):

if (preg_match(sprintf('~%s~i', $b), $this->message) === 1) {
    // ...
}

但正如@tino.codes 回答的那样,使用类似的功能stripos()就足够了。

于 2021-09-28T07:59:39.460 回答