Log in

View Full Version : replacing split() with explode()



megha_3000
10-02-2012, 08:08 AM
function dateconvert($date,$func){
if ($func == 1){ //insert conversion
list($day, $month, $year) = split('[/.-]', $date);
$year=trim($year);
$date = "$year-$month-$day";
return $date;
}
if ($func == 2){ //output conversion
list($year, $month, $day) =split('[-.]', $date);
if(trim($date)!=='') $date = "$day/$month/$year";
return $date;
}
}

This function is written with split() function. how can i replace it with explode(). because php 5.3.0 does not support split.

bernie1227
10-02-2012, 08:47 AM
an alternative to split(), is preg_split(), ie:


preg_split("/[|-|.|]/", $date)

megha_3000
03-28-2013, 06:33 AM
<?php
function dateconvert($date,$func){
if ($func == 1){ //insert conversion
list($day, $month, $year) = preg_split("/[|/|.|-|]/", $date);
$year=trim($year);
$date = "$year-$month-$day";
return $date;
}
if ($func == 2){ //output conversion
list($year, $month, $day) = preg_split("/[|-|.|]/", $date);
if(trim($date)!=='') $date = "$day/$month/$year";
return $date;
}
}
?>
i used the function preg_split in this way. but it doesn't work. can u please find out the error and recorrect it?

jscheuer1
03-28-2013, 06:55 AM
Try:


<?php
function dateconvert($date,$func){
if ($func == 1){ //insert conversion
list($day, $month, $year) = preg_split("/[\/.-]/", $date);
$year=trim($year);
$date = "$year-$month-$day";
return $date;
}
if ($func == 2){ //output conversion
list($year, $month, $day) = preg_split("/[-.]/", $date);
if(trim($date)!=='') $date = "$day/$month/$year";
return $date;
}
}
?>

megha_3000
03-28-2013, 07:08 AM
ALHAMDULILLAH Thank You so much. :)