Page 1 of 1

explode an array based on a Carriage Return Line Feed

Posted: Mon Sep 20, 2010 10:26 am
by IGGt
Hi guys,

Is there a way to explode an array based on a Carriage Return Line Feed?

--reason below
I am running a MySQL query, which I then put into an Array. One of those MySQL fields is a MEDIUMTEXT field and contains a string of data that looks like:
Mesg Time: 04:55 Mesg Date: 13/09/2010
For: company support
From: John
Company: companyName
Phone: 01234 567 890
Message Taken By: user 1
ORD 3483 on 13/09/2010 at 04:54
Was Relayed To:-
(1)user 100,Mob: 07808 123456
Call Successfully Relayed

I then Explode this into another Array using the : as the divider.

I then explode this further on a line by line basis in order to leave me with the just the data I need, which I can then display in a table.

My problem comes at the line
07808 123456
Call Successfully Relayed

I want to be left with:
07808 123456
Call Successfully Relayed

That is fine. By using the word 'Call' as the divider I had:
07808 123456
Successfully relayed

close enough for my needs. but then I discovered some calls that look like:
07808 123456
No Answer

I thought, that's fine, I'll split it using " " (space) and then check

Code: Select all

IF ($array[11][2] = "Call") {$result = $array[11][2].$array[11][3].$array[11][4]}; // Call Successfully relayed
ELSEIF {($array[11][2] = "No") {$result = $array[11][2].$array[11][3]}; // No Answer
but it doesn't work as there isn't actually a space between the two parts, so what I end up with is:
07808
123456 No
Answer

When I checked using a hex editor, I found there is a 0D0A in there (carriage return Line feed), not a space. And that is what's causing the problem. Is there a a way to filter these?

Re: explode an array based on a Carriage Return Line Feed

Posted: Mon Sep 20, 2010 3:10 pm
by McInfo
This splits a string on one or more vertical whitespace characters.

Code: Select all

$lines = preg_split('/\v+/', $string);
Do you have any control of the storage format? A more consistent format would be easier to work with. It would be even better if all those pieces of data were in separate columns in the database.

Re: explode an array based on a Carriage Return Line Feed

Posted: Tue Sep 21, 2010 3:23 am
by IGGt
cheers, that look slike what I am after.

Unfortunately, the database structure pre-dates me by some time, and is central to one of our core systems, so I am unable to modify that, otherwise you are quite right, in that it would be far better to split it all at the database level.


cheers,