Skip to content

Instantly share code, notes, and snippets.

@ArrayIterator
Last active September 8, 2023 03:55
Show Gist options
  • Save ArrayIterator/6f69d982a8f3042464a3c9eb253acd4c to your computer and use it in GitHub Desktop.
Save ArrayIterator/6f69d982a8f3042464a3c9eb253acd4c to your computer and use it in GitHub Desktop.
NMEA GPRMC - (GPRMC) PARSER
<?php
function parse_nmea(string $geo_str) : array
{
$geo_str = trim($geo_str);
$split = explode(",", $geo_str);
$split = array_map('trim', $split);
$lat = $split[3];
$lat_2 = abs(substr($lat, 0, 2));
$lat_3 = (abs(substr($lat, 2)) / 60);
$lon = $split[5];
$lon_2 = abs(substr($lon, 0, 3));
$lon_3 = (abs(substr($lon, 3)) / 60);
$utc = explode('.', $split[1])[0];
$date = $split['9'] ?? null;
$time = null;
if ($utc && !empty($split[9])) {
$h = substr($utc, 0, 2);
$i = substr($utc, 2, 2);
$s = substr($utc, 4, 2);
$d = substr($date, 0, 2);
$m = substr($date, 2, 2);
$y = substr($date, 4, 2);
$time = @mktime($h, $i, $s, $m, $d, $y)?:null;
}
return [
'data' => $geo_str,
'utc' => $utc,
'type' => 'GPRMC',
'status' => $split[2]??null,
'format_y' => $split[3]??null,
'format_x' => $split[5]??null,
'alpha_y' => $split[4]??null,
'alpha_x' => $split[6]??null,
'latitude' => $lat_2 + $lat_3,
'longitude' => $lon_2 + $lon_3,
'speed' => $split[7]??null,
'track' => $split[8]??null,
'utc_date' => $date,
'magvar' => $split[10],
'mag_ew' => trim($split[11] ?? ''),
'unix_time' => $time,
'date_utc' => $time ? date('Y-m-d H:i:s', $time) : '',
];
}
$gprmc = '$GPRMC,114530.000,A,0344.5185,N,09840.5572,E,0.0,116.1,181220,,,A/00000,00000/0,0,0,0/307267400//fd5/';
$nm = parse_nmea($gprmc);
print_r($nm);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment