Parse and grab certain words

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
Simon Angell
Forum Commoner
Posts: 45
Joined: Fri Jan 24, 2003 12:14 am

Parse and grab certain words

Post by Simon Angell »

Hi all.
some help on this would be greatly appreciated....

ok, i have file, the content of which is more or less like below..
IDN25900
TOP PRIORITY FOR IMMEDIATE BROADCAST
SYDNEY SEVERE THUNDERSTORM WARNING
BUREAU OF METEOROLOGY
NEW SOUTH WALES REGIONAL OFFICE Issued at 1703 on Saturday the 1st of
September 2001
This warning affects people in the following Local Government Area:

Wyong

This warning is current from 5:05pm until 6pm.

Storms are currently located to northwest of Wyong and are moving towards
the southeast. Locations that may be affected in the next hour include the
Wyong and The Entrance

Large hailstones, damaging winds and very heavy rainfall are possible.

The STATE EMERGENCY SERVICE advises that as storms approach people should:
* put vehicles under cover
* move indoors away from windows

During and after the storm people should:
* beware of fallen trees and power lines
* keep away from creeks and drains as you may be swept away

The RTA recommends motorists switch their lights & wipers on & slow down in
the wet.

If your house is damaged contact the State Emergency Service on 132 500 for
emergency assistance. Do not use the telephone during the storm.

TV CRAWL: Severe Thunderstorm Warning current for the Wyong Local
Government Area

MEDIA PLEASE NOTE: This warning will be updated within the next hour. The
Bureau and SES would appreciate it being broadcast regularly during this
period.
Now, i need to abbreviate that to 160 characters, and my current script that does it, is "clunky". It grabs the whole file, and abbreviates/deletes words etc etc.

I figure the has to be a better way, and such though it would be easier to say, parse the file for ceratin keywords, such as
"SYDNEY SEVERE THUNDERSTORM WARNING","Issued at 1703" ,"Saturday the 1st of September 2001", "Local Government Area:

Wyong
"
etc etc, and these keywords would be the basis of the abbreviation. and out put would look something like
Svr TS Warning 1703 1/09/01 for Wyong
Now, i think preg_match () is what im looking for, but i don't know how to construct the preg_match () function...

this is my thoughts on it..

Code: Select all

<?php
$file = "filename";
$warntype = preg_match($file, words to match);
//abb the words
$warntype[1] = str_replace("SEVERE THUNDER STORM WARNING", "Svr TS Warning", $warntype[1]);
trim ($warntype[1]);
print ($warntype[1]);
?>
is that heading in the right direction?
any help on getting this done and constructing the preg_match would be great
phait
Forum Commoner
Posts: 46
Joined: Wed Apr 07, 2004 4:41 am
Location: watford / leicester, UK

Post by phait »

hi,
do you have to abbreviate the whole file to 160 or just the top lines which seem to carry location and what the warning is about?

Also, do all the text files follow this format? - The reason i ask is that you could read the contents of the file line by line and you would know that line 1 contained the id number, the second line contained the priority and so on. That way you can decide what lines to use initially before you have to do any reg ex's to match against. For example, if you were always going to need the id number then there would be no need to check it against a format right?

You could try using the file() function that reads each line and adds it to an array, then you get certain lines back from the array and do your stuff with it.

Code: Select all

<?php

$lines = file('your/path/to/textfile.txt');

echo($lines[0]); //write out the first line which is the id number

?>
Maybe you could then assign vars to certain lines and perform checking against those?

You'd still have to do some regex's at some point but the data you'd be working with would be smaller and you would have a better idea of what would be expected and could check against that. This assumes that the text files all have the same format of course.

my 2p's worth
Simon Angell
Forum Commoner
Posts: 45
Joined: Fri Jan 24, 2003 12:14 am

Post by Simon Angell »

yeah, basically it would be theses lines
SYDNEY SEVERE THUNDERSTORM WARNING
BUREAU OF METEOROLOGY
NEW SOUTH WALES REGIONAL OFFICE Issued at 1703 on Saturday the 1st of
September 2001
This warning affects people in the following Local Government Area:

Wyong

This warning is current from 5:05pm until 6pm.


Unfortunatley they differ slightly in format, but ill have a play around with what you have suggested, thx
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Is this a one time thing? If so, you'd be better to do it by hand..
If not, and the content totally changes.. this will be EXTREMELY hard to do. It isn't like you're just trying to grab a value, heck, if you make a regex for it, the next time, if the words aren't in the right order, it might give you "the" and delete "storm" since they werent formatted the same..

Can you tell us what this is for, and why it must be automated?
Simon Angell
Forum Commoner
Posts: 45
Joined: Fri Jan 24, 2003 12:14 am

Post by Simon Angell »

Oh, its for getting the Info needed, and sending an SMS off to members subscribed, there is like 60 or so different warnings, with a few different formats. And yes it needs to be automated.... the above code works nicely, will have to develop it even more ;)
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

[php_man]str_replace[/php_man] can do it....

Code: Select all

<?php
$lookup = array('you', 'for');
$replace('u', '4');
$msg = str_replace($lookup, $replace, $msg);
?>
you just need to make a database of words to be looked up and replaced, then its matter of adding more words when you need to.
Post Reply