php 已弃用:不推荐使用函数 split()。如何重写这个语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3453915/
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
Deprecated: Function split() is deprecated. How to rewrite this statement?
提问by morpheous
I have the following statement which worked fine before PHP 5.3 using the split
function:
我有以下语句在 PHP 5.3 之前使用该split
函数运行良好:
list($year, $month, $day, $hour, $min, $sec) = split( '[: -]', $post_timestamp );
After upgrading to PHP 5.3, I get the Deprecated warning:
升级到 PHP 5.3 后,我收到已弃用警告:
Deprecated: Function split() is deprecated.
已弃用:不推荐使用函数 split()。
I am trying to parse a string with format like:
我正在尝试解析格式如下的字符串:
2010-08-10 23:07:58
2010-08-10 23:07:58
into its component parts.
进入其组成部分。
回答by Brandon Horsley
I think you want preg_split.
我想你想要preg_split。
list($year, $month, $day, $hour, $min, $sec) = preg_split('/[: -]/', $post_timestamp);
回答by Baqir Sardar
Just try to replace "split" with "explode" the newer version of PHP and MYSQL accept "explode" instead of "split"
只需尝试用“爆炸”替换“拆分”,较新版本的 PHP 和 MYSQL 接受“爆炸”而不是“拆分”
回答by Mchl
$dateTime = new DateTime('2010-08-10 23:07:58');
$year = $dateTime->format('Y');
$month = $dateTime->format('m');
You get the drill... Depending, on what you're going to do with it, using DateTime object might be more convenient than using six separate variables.
你得到了练习......根据你打算用它做什么,使用 DateTime 对象可能比使用六个单独的变量更方便。
回答by zerkms
var_dump(strptime($post_timestamp, '%Y-%m-%d %H:%M:%S'));