Jump to content

PHP / Expressions / Regex / Help!


FiremanSam

Recommended Posts

Hey all,

Wondering maybe someone can help me out, I am training to be a firefighter (I am not a coder) and am writing a little script for my local volunteer fire brigade to try help em out a little and I am stuck! It doesn't do much but grab the address out of a pager message but I have searched and tried a few ways to get this and now I am tired and just can't get my head around how to do this! Let me explain,

 

Below are 2 pager alert messages and I have a sore head of a time trying to extract the address and job details of the second message into a php string using Expressions/Regex...

 

Here are 2 example messages:

 

0571040 15:45:21 30-04-12  @@ALERT F546356345 THEB8 STRUC1 SMELL OF SMOKE AND ALARM OPERATING 900 SOME ROAD SOMESUBURB /CROSSSTREET1 RD //CROSSTREET2 AV M 99 A1 (429085) CTHEB CBOROS PT28 [THEB]

0571040 15:45:21 30-04-12  @@ALERT F546356345 THEB8 STRUC1 SMELL OF SMOKE AND ALARM OPERATING 4 / 900 SOME ROAD SOMESUBURB /CROSSSTREET1 RD //CROSSTREET2 AV M 99 A1 (429085) CTHEB CBOROS PT28 [THEB]

 

You will note the second address has 4 / 900 at the start or it could say Unit 4 / 900... and this is where my issue starts! The addresses can come in the 2 different formats, I have first one sorted with the code below but this address with no 4 / 900 some road just has me and my code below stumped. The extra / is just to complex a thing for me and my brain to handle! lol Please Help! :)

 

As you can see I use the first slash as the first cross street but in the second case above the first / is now a part of the address... Below is what I have so far:

 

function get_string_between2($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}

$fullstring = "$rawPage";

if ( strpos($fullstring, ' STRUC1 ')!== false )
{
$parsed = get_string_between2($fullstring, "STRUC1", "/");
}
$input = "$parsed";
preg_match('/([^0-9]+)(.*)/', $input, $matches);

$jobdet = "$matches[1]";

$jobadd = "$matches[2]";
Now this works fine for the top message and I get this as the result:

$jobdet =  SMELL OF SMOKE AND ALARM OPERATING

$jobadd =  900 SOME ROAD SOMESUBURB

$firstcrossstreet = /CROSSSTREET1 RD 

$secondcrossstreet = //CROSSSTREET2 AV 
For the second message it's all wrong with this the result:

$jobdet =  SMELL OF SMOKE AND ALARM OPERATING

$jobadd =  4 

$firstcrossstreet = / 900 SOME ROAD SOMESUBURB /CROSSSTREET1 RD 

$secondcrossstreet = //CROSSSTREET2 AV 

 

I know it's the / causing it but how can I make the code handle either case?

 

Any help would be greatly appreciated!

Thanks!

Link to comment
Share on other sites

The  "STRUC1" is always there. or I should say that there is always a 6 letter code. Its the job class which in this case means structure fire code1. I actually repeat the above code for the different job classes. After that is the job details in this case is "SMELL OF SMOKE AND ALARM OPERATING".  Then the address. umm yer I hope that made sense.  :)

Link to comment
Share on other sites

So if "SMELL OF SMOKE AND ALARM OPERATING" is a predetermined message, can you get the list of all possible messages, then just take whatever comes after one of those list as the address

Link to comment
Share on other sites

Its not a predetirmed message though, it could anything. We got one not long ago that said "INCIC3 CAT WITH HEAD STUCK IN FENCE PALINGS 999 SOMETHING ROAD SOMESUBURB /FIRST CROSS STREET //SECOND CROSS STREET" (umm poor cat but LOL) The only thing that is in the same place is the 6 letter job class. (Oh, Except the *EMR* one, thats 5 letters...) I'll list them all:

 

*EMR* = Emergency Medical Response - CODE 1

 

G&SC1 = Grass & Scrub Fire - CODE 1

G&SC3 = Grass & Scrub Fire - CODE 3

 

STRUC1 = Structure Fire - CODE 1

STRUC3 = Structure Fire - CODE 3

 

NONSC1 = Non Structure Fire - CODE 1

NONSC3 = Non Structure Fire - CODE 3

 

INCIC1 = Incident - CODE 1

INCIC3 = Incident - CODE 3

 

ALARC1 = Alarm - CODE 1

ALARC3 = Alarm - CODE 3

 

RESCC1 = Rescue - CODE 1

RESCC3 = Rescue - CODE 3

 

 

 

I have been starting from the 6 letter job class code (in the cat case incident code3) and ending at the fist slash to give me say string $a. Then with string $a I grab everything before the numbers as the job details and everything after the number upto the first slash as the address. It works with the top example but not the second with the extra /...

 

In the first example the first / means cross street no1

the // means cross street no.2

 

Also I said, I just repeat the code for the different job classes over and over (I not so smart to make it one expression that did it all) but the extra / in the second example screws my code up..

 

As I said I no coder, I just reading alot to get this far but now I stuck... I am not the best at explaining what I try and do as I not sure half the time myself but I do hope all this make sense! lol

Link to comment
Share on other sites

OK, I really wanted to get the details of each individual parameter, because it looks like there are several different codes in the text. In order to build a single expression I'd need to know if there are some variances not included in your examples. But I've made some guesses and produced the code below. Assuming all the messages have the same parameters, using the "example" strings you posted it appears to work.

 

function getDetails($string)
{
    $regEx  = "(\d+) (\d\d:\d\d:\d\d) (\d\d-\d\d-\d\d)  ";
    $regEx .= "@@ALERT ([^ ]*) ([^ ]*) ([^ ]*) (.*?)(?=/\w)";
    $regEx .= "/([^ ]* [^/]*)//([^ ]* [^(]*)";
    $regEx .= "\(([^\)]*)\) ([^ ]*) ([^ ]*) ([^ ]*) \[([^\]]*)\]";  // CTHEB CBOROS PT28 [THEB]
    preg_match("#{$regEx}#", $string, $match);
    return $match;
}

 

Using the two examples stings you posted the function will return the following for each

Array
(
    [0] => 0571040 15:45:21 30-04-12  @@ALERT F546356345 THEB8 STRUC1 SMELL OF SMOKE AND ALARM OPERATING 900 SOME ROAD SOMESUBURB /CROSSSTREET1 RD //CROSSTREET2 AV M 99 A1 (429085) CTHEB CBOROS PT28 [THEB]
    [1] => 0571040
    [2] => 15:45:21
    [3] => 30-04-12
    [4] => F546356345
    [5] => THEB8
    [6] => STRUC1
    [7] => SMELL OF SMOKE AND ALARM OPERATING 900 SOME ROAD SOMESUBURB 
    [8] => CROSSSTREET1 RD 
    [9] => CROSSTREET2 AV M 99 A1 
    [10] => 429085
    [11] => CTHEB
    [12] => CBOROS
    [13] => PT28
    [14] => THEB
)
Array
(
    [0] => 0571040 15:45:21 30-04-12  @@ALERT F546356345 THEB8 STRUC1 SMELL OF SMOKE AND ALARM OPERATING 4 / 900 SOME ROAD SOMESUBURB /CROSSSTREET1 RD //CROSSTREET2 AV M 99 A1 (429085) CTHEB CBOROS PT28 [THEB]
    [1] => 0571040
    [2] => 15:45:21
    [3] => 30-04-12
    [4] => F546356345
    [5] => THEB8
    [6] => STRUC1
    [7] => SMELL OF SMOKE AND ALARM OPERATING 4 / 900 SOME ROAD SOMESUBURB 
    [8] => CROSSSTREET1 RD 
    [9] => CROSSTREET2 AV M 99 A1 
    [10] => 429085
    [11] => CTHEB
    [12] => CBOROS
    [13] => PT28
    [14] => THEB
)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.