Skip to content

Instantly share code, notes, and snippets.

@scottgrayson
Created July 27, 2017 22:16
Show Gist options
  • Save scottgrayson/1eb22c82d0c4eb05ced2705672449e16 to your computer and use it in GitHub Desktop.
Save scottgrayson/1eb22c82d0c4eb05ced2705672449e16 to your computer and use it in GitHub Desktop.
loops and strings
#!/usr/bin/php
<?php
$response = "PNBX|%O=87%N=10%S=OK|%XLL=39.988773,-75.413552%XSN=Bus Center Rd%XSN=%DIST=m73|%XLL=39.990304,-75.410596%XSN=%XSN=Bus Center Rd%DIST=m239|%XLL=39.990411,-75.408906%XSN=Medical Dr%XSN=%DIST=m377|%XLL=39.985864,-75.412136%XSN=College Ave%XSN=W Chester Pike\:RN/PA-3///USA3%DIST=m395|%XLL=39.986115,-75.410542%XSN=Ellis Ave\:AX/Ellis Rd%XSN=W Chester Pike\:RN/PA-3///USA3%DIST=m419|%XLL=39.985758,-75.414813%XSN=%XSN=W Chester Pike\:RN/PA-3///USA3%DIST=m423|%XLL=39.986007,-75.410518%XSN=Ellis Ave\:AX/Ellis Rd%XSN=W Chester Pike\:RN/PA-3///USA3%DIST=m431|%XLL=39.985630,-75.414794%XSN=%XSN=W Chester Pike\:RN/PA-3///USA3%DIST=m436|%XLL=39.991259,-75.417594%XSN=Rockwood Farm Rd%XSN=Goshen Rd%DIST=m437|%XLL=39.985443,-75.412090%XSN=College Ave%XSN=Hickory Ln%DIST=m442||";
$str = strstr($response, '%XLL');
$arr = explode('|', $str);
$res = [];
foreach ($arr as $v) {
$matches = [];
preg_match_all('/%(\w+)=([^%]*)/', $v, $matches);
$keys = $matches[1];
if (!count($keys)) {
continue;
}
$combined = [];
foreach ($matches[2] as $index => $val) {
if ($val && isset($combined[$keys[$index]])) {
$combined[$keys[$index]] .= '|' . $val;
}
else if ($val) {
$combined[$keys[$index]] = $val;
}
}
$res []= $combined;
}
print_r($res);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment