igorgue
igorgue

Reputation: 18242

PHP split alternative?

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

Upvotes: 94

Views: 116956

Answers (8)

Ferdinand
Ferdinand

Reputation: 21

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

Upvotes: 0

Andre Cotelo
Andre Cotelo

Reputation: 21

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

Upvotes: 2

i.jolly
i.jolly

Reputation: 61

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 :)

Upvotes: 8

Alexandrw
Alexandrw

Reputation: 1329

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

Upvotes: 0

tplaner
tplaner

Reputation: 8461

  • 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.

Upvotes: 17

Sarfraz
Sarfraz

Reputation: 382606

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

Upvotes: 133

Addo Solutions
Addo Solutions

Reputation: 1637

Yes, I would use explode or you could use:

preg_split

Which is the advised method with PHP 6. preg_split Documentation

Upvotes: 1

salathe
salathe

Reputation: 51950

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.

Upvotes: 22

Related Questions