Extracting data from a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gugel
Forum Newbie
Posts: 1
Joined: Thu Aug 14, 2008 10:53 am

Extracting data from a string

Post by Gugel »

Hi there,

Any PHP gurus here to the rescue? I'm sure this is fairly straightforward, but I just can't seem to figure it out! Any help would be greatly appreciated!

OK, here's a sample of the data that's gonna be entered on a form. There's lot of junk we don't need, but we have 4 entries separated by a line break. I've bolded what we DO need.

00001740 29 v 04 breathe 0 take_a_breath 0 respire 0 suspire 3 021 * 00005041 v 0000 * 00004227 v 0000 + 03189605 a 0301 + 00819106 n 0303 + 04034949 n 0301 + 04200499 n 0105 + 00819106 n 0101 ^ 00004227 v 0103 ^ 00005041 v 0103 $ 00002325 v 0000 $ 00002573 v 0000 ~ 00002573 v 0000 ~ 00002724 v 0000 ~ 00002942 v 0000 ~ 00003826 v 0000 ~ 00004032 v 0000 ~ 00004227 v 0000 ~ 00005041 v 0000 ~ 00006735 v 0000 ~ 00007366 v 0000 ~ 00017051 v 0000 02 + 02 00 + 08 00 | draw air into, and expel out of the lungs; "I can breathe better when the air is clean"; "The patient is respiring"
00002325 29 v 01 respire 1 005 $ 00001740 v 0000 @ 02088655 v 0000 + 03189605 a 0101 + 00819106 n 0103 + 00818726 n 0101 01 + 02 00 | undergo the biomedical and metabolic processes of respiration by taking up oxygen and producing carbon monoxide
00002724 29 v 01 choke 0 002 @ 00001740 v 0000 + 13871855 n 0101 01 + 02 00 | breathe with great difficulty, as when experiencing a strong emotion
00003133 29 v 01 hyperventilate 1 003 $ 00002942 v 0000 @ 00077958 v 0000 + 00821785 n 0101 01 + 09 00 | produce hyperventilation in; "The nurses had to hyperventilate the patient"


Here's the output we're looking for:
v, breathe, draw air into, and expel out of the lungs
v, respire, undergo the biomedical and metabolic processes of respiration by taking up oxygen and producing carbon monoxide
v, choke, breathe with great difficulty, as when experiencing a strong emotion
v, hyperventilate, produce hyperventilation in


Thanks in advance!
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Extracting data from a string

Post by ghurtado »

Have you tried anything yet?
dancing dragon
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2008 4:57 am
Location: bed

Re: Extracting data from a string

Post by dancing dragon »

It would be nice if the data input method gave you the resulting data output all associated with variable names. But assuming you can't change that...

I would first look at all the different delimiters in the data string that you can use to split the string into tokens. What are they?

The first one people usually think of is white space. But white space alone won't work here because you have phrases that include spaces that you want as one token instead of each word as a token. So what are the other delimiters in the data string? Hint: punctuation marks.

The solution is probably going to include splitting the data string by a certain delimiter to get a set of tokens, then going to certain tokens and splitting them further by a different delimiter, and so on.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Extracting data from a string

Post by califdon »

Like with every other programming effort, the first and most important task is to define clearly what it is expected to do. Your post gives me no clue about what determines which parts of the data you want to extract! An example is of no help, since I don't know what governed your choice of that example. If you can tell us very specifically what determines which pieces you want to extract, we can probably help you do it.
Post Reply