PHP函数ereg_replace()已弃用

时间:2012-07-25 15:00:11

标签: php mysql

  

可能重复:
  Converting ereg expressions to preg

我有这个函数用于过滤器wrod(坏词)表单title / desc。现在我在页面中看到ereg_replace() is deprecated php错误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;
}

2 个答案:

答案 0 :(得分:4)

不要使用ereg_replace(),因为它已被弃用。使用preg_replace()。通过快速浏览一下您的功能,您能够简单地转换功能。

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

虽然纠正上述问题应该可以解决问题。如果这是生产,您应该强烈考虑禁用display_errors

答案 1 :(得分:0)

当您使用标题功能时,如果在发送实际输出之前,即使单个空格作为响应发送,也会出现错误

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

最好不要关闭php结束标记。检查你是否有空格和其他错误@Json已经给出了答案{@ 1}}。