Log in

View Full Version : Splitting a sting into individual chars



arataon
07-15-2012, 09:38 AM
Hi,

I have been trying to split up a number string with PHP into individual chars but have had no luck so far. I have used str_split and according to the manual what I'm doing should work, but it isn't.

Bascially what I have is this:




$dateFormat = str_split($row[DATE], 1);
echo $dateFormat[0]; echo $dateFormat[1]; echo "/"; echo $dateFormat[2]; echo $dateFormat[3]; echo "/"; echo $dateFormat[4]; echo $dateFormat[5]; echo $dateFormat[6]; echo $dateFormat[7];




$row[DATE] is a string for the date which comes out of the database, for example, it could be: 15072012. I need it to be in this format for other processes I have running in the background, but what I need for this process is for 15072012 to be re-formatted to 15/07/2012.

Any pointers or guidance would be appreciated.

Thanks! :-)

JShor
07-18-2012, 02:12 PM
I don't get it, the code is working just fine. I tested it like so:


$dateFormat = str_split("15072012", 1);
echo $dateFormat[0]; echo $dateFormat[1]; echo "/"; echo $dateFormat[2]; echo $dateFormat[3]; echo "/"; echo $dateFormat[4]; echo $dateFormat[5]; echo $dateFormat[6]; echo $dateFormat[7];


and it returned: 15/07/2012

Is it possible that your $row[DATE] is null in the instance that you tried it? Try looking at that as your problem.