Jump to content

RegEx - Lost in translation


Andre

Recommended Posts

I coded a script, which parses incoming data from an mailbox (imap via socket), from which I now need to extract the data. What is the best way to do this? I had several attempts with regular expressions but never got something well working...

I cutted at '\r\n' and had one array per datapackage which I then searched for keywords with strpos() [e.g. 'Date'] and then used substr() to get the data [e.g. 18.11.01 00:04:04]

Maybe someone here has a better solution!! The big problem is that some values don't have a fixed number of digits, which makes substr() not a very good choice...



[b]Desired data:[/b]
Date, Lc, Lat1 - Lon2, Pass duration, Altitude

[b]
Data Example [/b] (attention: Additional white spaced missing in this post.)

1234 Date : 18.11.01 00:04:04 LC : 1 IQ : 50
Lat1 : 0.997N Lon1 : 86.470E Lat2 : 7.013N Lon2 : 60.194E
Nb mes : 006 Nb mes>-120dB : 000 Best level : -124 dB
Pass duration : 236s NOPC : 3
Calcul freq : 401 652669.6 Hz Altitude : 0 m
00 46 02 00
00 00 00 00
00 240 00 00
00 03 195 195


1234 Date : 18.11.01 01:36:18 LC : A IQ : 08
Lat1 : 0.998N Lon1 : 86.502E Lat2 : 10.384N Lon2 : 44.491E
Nb mes : 003 Nb mes>-120dB : 000 Best level : -131 dB
Pass duration : 282s NOPC : 3
Calcul freq : 401 652667.5 Hz Altitude : 0 m
00 46 08 00
00 00 00 00
00 248 00 00
00 00 00 218


3214 Date : 18.11.01 03:09:08 LC : 2 IQ : 58
Lat1 : 1.004N Lon1 : 86.530E Lat2 : 0.103N Lon2 : 90.360E
Nb mes : 012 Nb mes>-120dB : 000 Best level : -124 dB
Pass duration : 788s NOPC : 4
Calcul freq : 401 652664.2 Hz Altitude : 0 m
00 45 238 02
181 41 74 86
165 43 90 150
173 93 212 05

(possibly one additional whitespace in front of the 4-digit numbers in front)
Link to comment
Share on other sites

If the strings you are parsing are of different size, substr() is probably not a good choice. Regex is about the only way I can think of. I assume the roadblock you hit w/ your regex previously is b/c of newline characters? But you stripped those out, so that shouldn't be the problem.
Link to comment
Share on other sites

OK, I am not good with RegEx, but at least it works:
[code]<?php

// Date, Lc, Lat1 - Lon2, Pass duration, Altitude

$str = '1234 Date : 18.11.01 00:04:04 LC : 1 IQ : 50
Lat1 : 0.997N Lon1 : 86.470E Lat2 : 7.013N Lon2 : 60.194E
Nb mes : 006 Nb mes>-120dB : 000 Best level : -124 dB
Pass duration : 236s NOPC : 3
Calcul freq : 401 652669.6 Hz Altitude : 0 m
00 46 02 00
00 00 00 00
00 240 00 00
00 03 195 195';

preg_match('/Date :(.*)LC :(.*)IQ :(.*)Lat1 :(.*)Lon1 :(.*)Lon2 :(.*)Nb mes :(.*)Pass duration :(.*)NOPC(.*)Altitude :(.*) m/s', $str, $m);

for($i=0; $i<count($m); $i++) {
   $m[$i] = trim($m[$i]);
}

$info = array (
   'date' => $m[1],
   'lc'   => $m[2],
   'lat1' => $m[4],
   'lon2' => $m[6],
   'pd'   => $m[8],
   'alt'  => $m[10],
);

echo '<pre>';
print_r($info);

?>[/code]
[code]Array
(
    [date] => 18.11.01 00:04:04
    [lc] => 1
    [lat1] => 0.997N
    [lon2] => 60.194E
    [pd] => 236s
    [alt] => 0
)[/code]
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.