Forum Moderators: coopster

Message Too Old, No Replies

parsing lines of a string

         

t3pp3rs

2:07 am on Mar 3, 2011 (gmt 0)

10+ Year Member



Hello All,

I would like to have an idea on how to parse this string line by line.

ETHICS - Business Ethics
3Mr. TEACHER
IT 4-B

Sat7:30 AM10:30 AM
QUAMGT - Quality Management
3Mr. TEACHER
IT 4-B

Tue2:30 PM5:30 PM
ELEC 3 - Elective III
3
IT 4-B

Fri12:30 PM5:30 PM

I need to get all the date and time (5th line)so that I can make a class schedule. Then, the first 3 lines of every 5 lines will be the content per schedule.

Thanks in advance...

httpwebwitch

8:55 pm on Mar 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, t3pp3rs!

Why is the data so awkwardly formatted?

It looks like what you get when you cut & paste rendered XML into Text, with all the entities removed. Yuck.

You can parse this using a while() loop, reading line by line, and keep a $linecounter++ going inside the loop.
Take the modulus 5 of $linecounter, use that as an "if" condition to parse the line using your choice of string functions.

Sorry I'm not going to offer pseudo-code.

It looks like the sort of thing that a mean-spirited professor would toss out as a homework assignment for a beginner programming class

g1smd

8:59 pm on Mar 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If the data were in CSV format, you could parse it out using any one of the many pre-written script fragments, classes and modules that already exist.

t3pp3rs

11:46 pm on Mar 3, 2011 (gmt 0)

10+ Year Member



Hi! Thanks for your replies...

Actually, this is a part of our thesis... I do not have any programming prowess.

I've tried to google some scripts but cannot find any...

The data is copied then paste on a text box then it should be parsed...

Anyways, Thank you all...

coopster

12:00 am on Mar 4, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There are a number of ways to approach this but if the content is going to be fixed, meaning segments of 5 lines per class schedule, then you could combine a series of file reading and counters. Or, if you knew that there would always be a blank line after the class information and prior to the last line which is going to be the class schedule, ... well, you get the picture. I guess what is important here is understanding what the variable "input" may look like, how it is laid out. Without some logical understanding of that portion, you're battle is uphill from the get go ;)

You can use PHP file reading functions to bring the data in to memory, then you will need to start parsing. So, let's start there. Here is an example of how you can read the data in:
$file = 'a1.txt'; // your filename here located in same dir as this script 
$trimmed = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($trimmed as $line_num => $line) {
print "Line #<b>{$line_num}</b> : {$line}<br>\n";
}

g1smd

12:16 am on Mar 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A few CSV parsers in this list: [code.google.com...]