Deprecated: Function ereg() is deprecated [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-10 17:35:23

问题


Possible Duplicate:
How can I convert ereg expressions to preg in PHP?

My contact form is othervise working but I keep getting the following error:

Deprecated: Function ereg() is deprecated in/home/.....

I'm really lost here but I figure this is the part that needs some adjusting.

    if ( empty($_REQUEST['name']) ) {
    $pass = 1;
    $alert .= $emptyname;
} elseif ( ereg( "[][{}()*+?.\\^$|]", $_REQUEST['name'] ) ) {    
    $pass = 1;
    $alert .= $alertname;
}
if ( empty($_REQUEST['email']) ) {
    $pass = 1;
    $alert .= $emptyemail;
} elseif ( !eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]   {2,3})$", $_REQUEST['email']) ) {
    $pass = 1;
    $alert .= $alertemail;
}
if ( empty($_REQUEST['message']) ) {
    $pass = 1;
    $alert .= $emptymessage;
} elseif ( preg_match( "[][{}()*+?\\^$|]", $_REQUEST['message'] ) ) {
    $pass = 1;
    $alert .= $alertmessage;
}

Finding a solution would be highly appreciated


回答1:


You must use preg_match instead of ereg because the last one is deprecated.

Replacing it is not a big deal:

ereg( "[][{}()*+?.\\^$|]", $_REQUEST['name'] )

will become:

preg_match( "/[][{}()*+?.\\^$|]/", $_REQUEST['name'] )

p.s. I had to modify more than one hundred files while I was porting my old project to PHP 5.3 to avoid manually modifying I've used following script to do it for me:

function replaceEregWithPregMatch($path) {
    $content = file_get_contents($path);
    $content = preg_replace('/ereg\(("|\')(.+)(\"|\'),/',
                            "preg_match('/$2/',",
                            $content);
    file_put_contents($path, $content);
}

I hope it helps.




回答2:


The function ereg() is deprecated and should not be used any more. The documentation tells you what to do (to use preg_match instead).




回答3:


Like you said - no bigie, it works like a charm:

if ( empty($_REQUEST['name']) ) {
    $pass = 1;
    $alert .= $emptyname;
} elseif ( preg_match( "/[][{}()*+?.\\^$|]/", $_REQUEST['name'] ) ) {  
    $pass = 1;
    $alert .= $alertname;
}
if ( empty($_REQUEST['email']) ) {
    $pass = 1;
    $alert .= $emptyemail;
} elseif ( !preg_match("#^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$#i", $_REQUEST['email']) ) {
    $pass = 1;
    $alert .= $alertemail;
}
if ( empty($_REQUEST['message']) ) {
    $pass = 1;
    $alert .= $emptymessage;
} elseif ( preg_match( "/[][{}()*+?\\^$|]/", $_REQUEST['message'] ) ) {
    $pass = 1;
    $alert .= $alertmessage;
}

Thank you guys



来源:https://stackoverflow.com/questions/13580784/deprecated-function-ereg-is-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!