fork(3) download
  1. <?php
  2.  
  3. function get_coordinates_array($entity_gps) {
  4. $items = array();
  5. '/([NSWE])(\\d{1,2})[^\\d](\\d{1,2})[^\\d]([\\d\\.]{1,10})[^\\d\\s]/ui',
  6. $entity_gps, $items, PREG_SET_ORDER);
  7. if ($res === 2) {
  8. return array(
  9. "lat" => array_slice($items[0], 1, 4),
  10. "lng" => array_slice($items[1], 1, 4)
  11. );
  12. } else {
  13. return null;
  14. }
  15. }
  16.  
  17. //testando
  18. $entity_gps = 'N40°11\'43.44" W8°25\'1.31"';
  19. $coordinatesArr = get_coordinates_array($entity_gps);
  20. echo $entity_gps . "\n";
  21. if (is_null($coordinatesArr)) {
  22. echo 'Entrada inválida!';
  23. } else {
  24. print_r($coordinatesArr);
  25. }
  26.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
N40°11'43.44" W8°25'1.31"
Array
(
    [lat] => Array
        (
            [0] => N
            [1] => 40
            [2] => 11
            [3] => 43.44
        )

    [lng] => Array
        (
            [0] => W
            [1] => 8
            [2] => 25
            [3] => 1.31
        )

)