94

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

8 Answers 8

133

explode is an alternative. However, if you meant to split through a regular expression, the alternative is preg_split instead.

Sign up to request clarification or add additional context in comments.

4 Comments

I disagree - explode is not the alternative, since it does not perform the same function as split, that is: to split a string by a regular expression. For that purpose, use preg_split.
@nickf: That's rightly pointed, i should have added that initially on. Thanks
In various languages, the split method does not always accept regular expressions. In .NET, Python, the String.Split() / str.split() methods only accept a fixed, literal string as a needle. Thus, explode is correct. For those coming from Java, JavaScript, Ruby, preg_split might turn out the solution.
It's compatible with Visual Basic .NET's String.Split(). Nice
22

split is 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.

If you do not need the regular expression functionality, then explode is a very good choice (and would have been recommended over split even 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.

Comments

17
  • preg_split if you need to split by regular expressions.
  • str_split if you need to split by characters.
  • explode if you need to split by something simple.

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 function in the manual and it will tell you alternatives.

Comments

8

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

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

How was split() used

<?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

How explode() can be used

<?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

Here is how we can use it :)

Comments

2

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

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

Output: Get this var

Comments

1

Yes, I would use explode or you could use:

preg_split

Which is the advised method with PHP 6. preg_split Documentation

Comments

0

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

Comments

0

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

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

<?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;
  }
}
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.