0

I have a coordinate in the format

43-09-42.0000N

I want to split it into its component parts

43 09 42.000 N

I'm using.

$parts = preg_split('/[-]/', $LON);

and it gives me

Array ( [0] => 43 [1] => 09 [2] => 42.0000N )

But I can't figure out who to get the Letter on the end to spilt out.

Any help appreciated I am a complete dope when it comes to regex.

MB.
  • 723
  • 1
  • 11
  • 28

2 Answers2

1
$string = '43-09-42.0000N';

$result = sscanf($string, '%d-%d-%[0-9.]%s');
var_dump($result);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • your answer works perfectly thanks ?? sscanf($string, '%d-%d-%[0-9.]%s', $deg, $min, $sec,$dir); <= would that let me pass back parameters ?? – MB. Feb 02 '14 at 23:27
  • That, or `list($deg, $min, $sec, $dir) = sscanf($string, '%d-%d-%[0-9.]%s');` – Mark Baker Feb 03 '14 at 00:00
1

If you want to split it (which could be useful if the format changes) you could use something like:

$parts = preg_split('/-|(?<=\d)(?=[a-z])|(?<=[a-z])(?=\d)/i', $LON);

If the format doesn't change you are better off using something like Mark's answer.

If the format changes slightly you can use a regex match which allows for some variations.

Community
  • 1
  • 1
Qtax
  • 33,241
  • 9
  • 83
  • 121