PHP - how to merge two GPX files (XML merge, PHP GPX analyzer)
Hello,
I have a PHP programmed in PHP that reads data from a GPX file via SimpleXML - I analyze speed, length, height, etc. All works fine, but I have the problem that I do not know how ideally or rather simply solve the merging of two GPX files and maybe more.
GPX files have the following structure:
All I need is a PHP script to remove the footer at the first file and XML Declaration at the second header. Can you advise anyone? Thanks
Hi,
if you settle for a dumber variant that removes the footer from the first GPX file and the second header, then merging GPX files using PHP would do the following:
PS: I have verified that it works (I just had to edit this publishing version and it is not verified)
I have a PHP programmed in PHP that reads data from a GPX file via SimpleXML - I analyze speed, length, height, etc. All works fine, but I have the problem that I do not know how ideally or rather simply solve the merging of two GPX files and maybe more.
GPX files have the following structure:
< ?xml version="1.0" encoding="UTF-8"?>
< gpx version="1.0">
< name>Gpx track< /name>
< trk>< name>Track< /name>< number>1< /number>< trkseg>
..............
..............
..............
< trkpt lat="51.999999" lon="15.77777">< ele>600.0< /ele>< time>2020-03-08T10:42:31+0100< /time>< /trkpt>
..............
..............
..............
< /trkseg>< /trk>
< /gpx>
All I need is a PHP script to remove the footer at the first file and XML Declaration at the second header. Can you advise anyone? Thanks
REPLY
Hi,
if you settle for a dumber variant that removes the footer from the first GPX file and the second header, then merging GPX files using PHP would do the following:
// merge two gpx files
public function gpxMerge($file_one, $file_two)
{
$file_one = file_get_contents($file_one);
$file_two = file_get_contents($file_two);
$lines = explode("\n", $file_one);
$exclude = array();
foreach ($lines as $line) {
if (strpos($line, '< /trkseg>< /trk>')) {
continue;
} elseif (strpos($line, 'gpx>')) {
continue;
}
$exclude[] = $line;
}
$file_one = implode("\n", $exclude);
$lines = explode("\n", $file_two);
$exclude = array();
foreach ($lines as $line) {
if (strpos($line, 'version')) {
continue;
} elseif (strpos($line, '< name>Gpx track< /name>')) {
continue;
} elseif (strpos($line, '< trk>< name>Track< /name>< number>1< /number>< trkseg>')) {
continue;
}
$exclude[] = $line;
}
$file_two = implode("\n", $exclude);
return $file_one . $file_two;
PS: I have verified that it works (I just had to edit this publishing version and it is not verified)