php 已弃用:不推荐使用函数 ereg()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13580784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:43:06  来源:igfitidea点击:

Deprecated: Function ereg() is deprecated

phpformsdeprecatederegiereg

提问by no0ne

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

可能的重复:
如何在 PHP 中将 ereg 表达式转换为 preg?

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

我的联系表格可以正常工作,但我不断收到以下错误:

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

已弃用:函数 ereg() 在/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

找到解决方案将不胜感激

回答by Leri

You must use preg_matchinstead of eregbecause the last one is deprecated.

您必须使用preg_match而不是ereg因为最后一个已被弃用。

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:

ps 在将旧项目移植到 PHP 5.3 时,我不得不修改一百多个文件以避免手动修改我使用以下脚本为我做这件事:

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

I hope it helps.

我希望它有帮助。

回答by Kai

The function ereg()is deprecated and should not be used any more. The documentationtells you what to do (to use preg_matchinstead).

该函数ereg()已被弃用,不应再使用。该文档告诉您要做什么(改为使用preg_match)。

回答by no0ne

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

谢谢你们