php 已弃用:不推荐使用函数 split()。如何修正这个说法?

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

Deprecated: Function split() is deprecated. How to fix this statement?

php

提问by Chris45234325

I have the following statement which worked fine before PHP 5.3 using the split function:

我有以下语句在 PHP 5.3 之前使用 split 函数运行良好:

$command = split (" ", $tag[1]);

After upgrading to PHP 5.3, I get the Deprecated warning:

升级到 PHP 5.3 后,我收到已弃用警告:

Deprecated: Function split() is deprecated.

回答by user428517

Use explode:

使用explode

$command = explode(" ", $tag[1]);

This is the standard solution for this case.

这是这种情况的标准解决方案。

If you need to match on a regular expression (rather than something simple like a space), use preg_split. It's slower than explode, so there's no reason to use it unless you needa regex.

如果您需要匹配正则表达式(而不是像空格这样简单的东西),请使用preg_split. 它比 慢explode,因此除非您需要正则表达式,否则没有理由使用它。

BTW to do the opposite (join array elements into a string), use implode.

顺便说一句,做相反的事情(将数组元素连接成一个字符串),使用implode.

回答by Tobias Golbs

Well the first thing someone should do is checking the documenation: split

好吧,某人应该做的第一件事就是检查文档:拆分

It is recommended to use preg_splitor explode

建议使用preg_split爆炸

回答by Hristo Valkanov

From thispage:

这个页面:

Tip

提示

split() is deprecated as of PHP 5.3.0. preg_split()is the suggested alternative to this function. If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.

自 PHP 5.3.0 起不推荐使用 split()。preg_split()是此函数的建议替代方案。如果您不需要正则表达式的强大功能,使用explode() 会更快,这不会产生正则表达式引擎的开销。

If you are going to split in " " you might consider explode to be a better alternative.

如果您打算拆分为“”,您可能会认为爆炸是更好的选择。