-3

可能重复:
将 ereg 表达式转换为 preg

我有这个功能用于过滤 wrod(坏词)形式的标题/描述。现在我在我的页面中看到了ereg_replace() is deprecatedphp 错误Deprecated: Function ereg_replace() is deprecated in C:\xampp\htdocs\share\configs\functions.php on line 2750和这个。Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\share\configs\functions.php:2747) in C:\xampp\htdocs\share\modules\signup\signup.php on line 31什么问题?如何解决这个问题?

这是我的功能:

    function filter_title($str)
{
    global $config;

    if ( $config['word_filters'] == '1' ) { $str = filterBadWords ( $str ); }
    if ( $config['word_filters'] == '0' ) { $str=mysql_real_escape_string($str); }
    //if ( $config['word_filters'] == '1' ) { $str = filterBadWords ( urlencode($str) ); }

    //$newstr=str_replace('"',"'",$str);
    $pats1 ='\'';  $repls1 ="";
    $pat[] ='\"';  $repl[] ="";
    $pat[] ='\&';  $repl[] =" and ";
    $pat[] ='\~';  $repl[] ="";
    $pat[] ='\@';  $repl[] ="";
    $pat[] ='\#';  $repl[] ="";
    $pat[] ='\$';  $repl[] ="";
    $pat[] ='\%';  $repl[] ="";
    $pat[] ='\^';  $repl[] ="";
    $pat[]='\*';  $repl[]="";
    $pat[]='\(';  $repl[]="";
    $pat[]='\)';  $repl[]="";
    $pat[]='\+';  $repl[]=" ";
    $pat[]='\`';  $repl[]="";
    $pat[]='\=';  $repl[]="";
    $pat[]='\!';  $repl[]="";
    $pat[]='\[';  $repl[]="";
    $pat[]='\]';  $repl[]="";
    $pat[]='\{';  $repl[]="";
    $pat[]='\}';  $repl[]="";
    $pat[]='\;';  $repl[]="";
    $pat[]='\:';  $repl[]="";
    $pat[]='\.';  $repl[]="";
    $pat[]='\/';  $repl[]="";
    $pat[]='\?';  $repl[]="";
    $pat[]='\<';  $repl[]="";
    $pat[]='\>';  $repl[]="";
    //$pat[]='\_';  $repl[]=" ";
    $pat[]="\\\\"; $repl[]="";
    $pat[]='\|';  $repl[]="";
    $pat[]='\,';  $repl[]="";
    $pat[]='\0x';  $repl[]="";

    $newstr = ereg_replace($pats1, $repls1, $str);    // ---> 2747 <---     
    for($i=0;$pat[$i];$i++) {
    $newstr = ereg_replace($pat[$i], $repl[$i], $newstr); // ---> 2750 <---
    }

    $newstr = preg_replace('/\s\s+/', ' ', $newstr); 

    return trim ( $newstr );
}

这个我的 filter_descr 函数:

function filter_descr($str)
    {
        //$str=mysql_real_escape_string($str);
        global $config;

        if ( $config['word_filters'] == '1' ) { $str = filterBadWords ( $str ); }
        if ( $config['word_filters'] == '0' ) { $str = mysql_real_escape_string($str); }
        //if ( $config['word_filters'] == '1' ) { $str = filterBadWords ( urlencode ( $str ) ); }

        //$newstr=str_replace('"',"'",$str);
        $pats1 = "\'";  $repls1 ="&#39;";
        $pat[] = '\"';  $repl[] ="&#34;";
        $pat[] = '\&';  $repl[] ="&amp;";
        //$pat[] ='\~';  $repl[] ="";
        //$pat[] ='\@';  $repl[] ="";
        //$pat[] ='\#';  $repl[] ="";
        $pat[] = '\$';  $repl[] ="";
        $pat[] = '\%';  $repl[] ="";
        $pat[] = '\{';  $repl[] ="";
        $pat[] = '\}';  $repl[]="";
        $pat[] = '\`';  $repl[]="";
        //$pat[]='\/';  $repl[]="";
        $pat[] = '\<';  $repl[]="";
        $pat[] = '\>';  $repl[]="";
        //$pat[]="\\\\"; $repl[]="";
        $pat[] = '\|';  $repl[]="";
        //$pat[]="\n";  $repl[]="<br>";
        //$pat[]='\r';  $repl[]="<br>";
        $pat[] = '\0x';  $repl[]="";

        $newstr = ereg_replace($pats1, $repls1, $str);

        for($i=0;$pat[$i];$i++) {
        $newstr=ereg_replace($pat[$i], $repl[$i], $newstr);
        }

        $newstr = preg_replace('/\s\s+/', ' ', $newstr);

        return trim ( $newstr );
    } 

PHP 注册(第 31 行评论):

session_start();
include('../../configs/config.php');

$smarty->assign('country', set_country_box(filter_title($_REQUEST['fcountry'])));
//if somebody is logged in and active, we don't need to see the registration page
if ($_SESSION["USERNAME"]!="" && $_SESSION["IS_ACTIVE"]=="1")
{
    header("Location: $config[base_url]/main");
    exit;
}

//if Cancel is pressed
if (filter_descr($_REQUEST[scancel])!="")
{
    header("Location: $config[base_url]/main"); // ----> LINE 31 <----
    exit;
}
4

2 回答 2

4

不要使用ereg_replace(),因为它已被弃用。使用preg_replace(). 快速浏览一下您的函数,您应该能够简单地转换函数。

至于标头已经发送错误。这可能是由于在您调用之前输出了已弃用的警告消息header()

尽管更正上述内容应该可以解决问题。如果这是生产,您应该强烈考虑禁用display_errors

于 2012-07-25T15:01:41.533 回答
0

每当您使用标头功能时,即使在发送实际输出之前发送了一个空格作为响应,您也会收到错误消息

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\share\configs\functions.php:2747)

最好不要关闭 php 结束标记。检查你有没有空格和其他错误@Json 已经给出了答案preg_replace()

于 2012-07-25T15:06:59.687 回答