php 函数 ereg_replace() 已弃用 - 如何清除此错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3132844/
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 08:47:53  来源:igfitidea点击:

Function ereg_replace() is deprecated - How to clear this bug?

phpdeprecatedereg

提问by Pradip

I have written following PHP code:

我编写了以下 PHP 代码:

$input="menu=1&type=0&";

print $input."<hr>".ereg_replace('/&/', ':::', $input);

After running above code, it gives following warning,

运行上面的代码后,它给出以下警告,

Deprecated: Function ereg_replace() is deprecated

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

How can I resolve this warning.

我该如何解决此警告。

回答by Quentin

Switch to preg_replaceDocsand update the expression to use preg syntax (PCRE) instead of ereg syntax (POSIX) where there are differencesDocs(just as it says to do in the manual for ereg_replaceDocs).

切换到preg_replaceDocs并更新表达式以使用 preg 语法 (PCRE) 而不是 ereg 语法 (POSIX) ,其中Docs存在差异(就像它在ereg_replaceDocs手册中所说的那样)。

回答by Krishna Tripathee

print $input."<hr>".ereg_replace('/&/', ':::', $input);

becomes

变成

print $input."<hr>".preg_replace('/&/', ':::', $input);

More example :

更多示例:

$mytext = ereg_replace('[^A-Za-z0-9_]', '', $mytext );

is changed to

改为

$mytext = preg_replace('/[^A-Za-z0-9_]/', '', $mytext );

回答by Mark Baker

change the call to ereg_replace to use preg_replaceinstead

呼叫改变ereg_replace到使用的preg_replace代替

回答by Amadan

http://php.net/ereg_replacesays:

http://php.net/ereg_replace说:

Note: As of PHP 5.3.0, the regex extension is deprecated in favor of the PCRE extension.

注意:从 PHP 5.3.0 开始,regex 扩展被弃用,取而代之的是PCRE 扩展

Thus, preg_replaceis in every way better choice. Note there are some differences in pattern syntax though.

因此,preg_replace在各方面都是更好的选择。请注意,虽然模式语法存在一些差异。

回答by Darko Kenda

Hereis more information regarding replacing ereg_replace with preg_replace

以下是有关用 preg_replace 替换 ereg_replace 的更多信息

回答by Wevah

IIRC they suggest using the preg_functions instead (in this case, preg_replace).

IIRC 他们建议改用这些preg_函数(在本例中为preg_replace)。