php PHP拆分替代?

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

PHP split alternative?

php

提问by igorgue

PHP is telling me that split is deprecated, what's the alternative method I should use?

PHP 告诉我 split 已被弃用,我应该使用什么替代方法?

回答by Sarfraz

explodeis an alternative. However, if you meant to split through a regular expression, the alternative is preg_splitinstead.

explode是一个替代方案。但是,如果您打算通过正则表达式进行拆分,则替代方案是preg_split

回答by salathe

splitis deprecated since it is part of the family of functions which make use of POSIX regular expressions; that entire family is deprecated in favour of the PCRE (preg_*) functions.

split已弃用,因为它是使用 POSIX 正则表达式的函数系列的一部分;整个系列都被弃用,以支持 PCRE ( preg_*) 函数。

If you do not need the regular expression functionality, then explodeis a very good choice (and would have been recommended over spliteven if that were not deprecated), if on the other hand you do need to use regular expressions then the PCRE alternate is simply preg_split.

如果您不需要正则表达式功能,那么这explode是一个非常好的选择(split即使没有被弃用,也会被推荐),另一方面,如果您确实需要使用正则表达式,那么 PCRE 替代方案就是preg_split.

回答by tplaner

  • preg_splitif you need to split by regular expressions.
  • str_splitif you need to split by characters.
  • explodeif you need to split by something simple.
  • preg_split如果您需要通过正则表达式拆分。
  • str_split如果您需要按字符拆分。
  • explode如果你需要用简单的东西来分割。

Also for the future, if you ever want to know what PHP wants you to use if something is deprecated you can always check out the functionin the manual and it will tell you alternatives.

同样是为了将来,如果您想知道 PHP 在不推荐使用的情况下希望您使用什么,您可以随时查看手册中的功能,它会告诉您替代方案。

回答by i.jolly

I want to clear here that preg_split();is far away from it but explode();can be used in similar way as split();

我想在这里清除preg_split();远离它但explode();可以以类似方式使用的split();

following is the comparison between split();and explode();usage

以下是split();explode();用法的比较

How was split() used

split() 是如何使用的

<?php

$date = "04/30/1973";
list($month, $day, $year) = split('[/.-]', $date);
echo $month; // foo
echo $day; // *
echo $year;

?>

URL: http://php.net/manual/en/function.split.php

网址:http: //php.net/manual/en/function.split.php

How explode() can be used

如何使用explode()

<?php

$data = "04/30/1973";
list($month, $day, $year) = explode("/", $data);
echo $month; // foo
echo $day; // *
echo $year;

?>

URL: http://php.net/manual/en/function.explode.php

网址:http: //php.net/manual/en/function.explode.php

Here is how we can use it :)

这是我们如何使用它:)

回答by Andre Cotelo

You can use the easier function preg_match instead, It's better and faster than all of the other ones.

您可以改用更简单的函数 preg_match,它比所有其他函数都更好更快。

$var = "<tag>Get this var</tag>";
preg_match("/<tag>(.*)<\/tag>/", $var , $new_var);
echo $new_var['1']; 

Output: Get this var

输出: Get this var

回答by Addo Solutions

Yes, I would use explode or you could use:

是的,我会使用爆炸,或者你可以使用:

preg_split

preg_split

Which is the advised method with PHP 6. preg_split Documentation

这是 PHP 6 推荐的方法。 preg_split 文档

回答by Alexandrw

If you want to split a string into words, you can use explode() or str_word_count().

如果要将字符串拆分为单词,可以使用explode() 或str_word_count()。

回答by Ferdinand

Had the same issue, but my code must work on both PHP 5 & PHP 7..

有同样的问题,但我的代码必须同时适用于 PHP 5 和 PHP 7 ..

Here is my piece of code, which solved this.. Input a date in dmY format with one of delimiters "/ . -"

这是我的一段代码,它解决了这个问题..输入一个带有分隔符“/.-”的 dmY 格式的日期

<?php
function DateToEN($date){
  if ($date!=""){
    list($d, $m, $y) = function_exists("split") ?  split("[/.-]", $date) : preg_split("/[\/\.\-]+/", $date);
    return $y."-".$m."-".$d;
  }else{
    return false;
  }
}
?>